ptrace.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595
  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 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 X86_BTS
  501. static int ptrace_bts_get_size(struct task_struct *child)
  502. {
  503. if (!child->thread.ds_area_msr)
  504. return -ENXIO;
  505. return ds_get_bts_index((void *)child->thread.ds_area_msr);
  506. }
  507. static int ptrace_bts_read_record(struct task_struct *child,
  508. long index,
  509. struct bts_struct __user *out)
  510. {
  511. struct bts_struct ret;
  512. int retval;
  513. int bts_end;
  514. int bts_index;
  515. if (!child->thread.ds_area_msr)
  516. return -ENXIO;
  517. if (index < 0)
  518. return -EINVAL;
  519. bts_end = ds_get_bts_end((void *)child->thread.ds_area_msr);
  520. if (bts_end <= index)
  521. return -EINVAL;
  522. /* translate the ptrace bts index into the ds bts index */
  523. bts_index = ds_get_bts_index((void *)child->thread.ds_area_msr);
  524. bts_index -= (index + 1);
  525. if (bts_index < 0)
  526. bts_index += bts_end;
  527. retval = ds_read_bts((void *)child->thread.ds_area_msr,
  528. bts_index, &ret);
  529. if (retval < 0)
  530. return retval;
  531. if (copy_to_user(out, &ret, sizeof(ret)))
  532. return -EFAULT;
  533. return sizeof(ret);
  534. }
  535. static int ptrace_bts_clear(struct task_struct *child)
  536. {
  537. if (!child->thread.ds_area_msr)
  538. return -ENXIO;
  539. return ds_clear((void *)child->thread.ds_area_msr);
  540. }
  541. static int ptrace_bts_drain(struct task_struct *child,
  542. long size,
  543. struct bts_struct __user *out)
  544. {
  545. int end, i;
  546. void *ds = (void *)child->thread.ds_area_msr;
  547. if (!ds)
  548. return -ENXIO;
  549. end = ds_get_bts_index(ds);
  550. if (end <= 0)
  551. return end;
  552. if (size < (end * sizeof(struct bts_struct)))
  553. return -EIO;
  554. for (i = 0; i < end; i++, out++) {
  555. struct bts_struct ret;
  556. int retval;
  557. retval = ds_read_bts(ds, i, &ret);
  558. if (retval < 0)
  559. return retval;
  560. if (copy_to_user(out, &ret, sizeof(ret)))
  561. return -EFAULT;
  562. }
  563. ds_clear(ds);
  564. return end;
  565. }
  566. static int ptrace_bts_config(struct task_struct *child,
  567. long cfg_size,
  568. const struct ptrace_bts_config __user *ucfg)
  569. {
  570. struct ptrace_bts_config cfg;
  571. int bts_size, ret = 0;
  572. void *ds;
  573. if (cfg_size < sizeof(cfg))
  574. return -EIO;
  575. if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
  576. return -EFAULT;
  577. if ((int)cfg.size < 0)
  578. return -EINVAL;
  579. bts_size = 0;
  580. ds = (void *)child->thread.ds_area_msr;
  581. if (ds) {
  582. bts_size = ds_get_bts_size(ds);
  583. if (bts_size < 0)
  584. return bts_size;
  585. }
  586. cfg.size = PAGE_ALIGN(cfg.size);
  587. if (bts_size != cfg.size) {
  588. ret = ptrace_bts_realloc(child, cfg.size,
  589. cfg.flags & PTRACE_BTS_O_CUT_SIZE);
  590. if (ret < 0)
  591. goto errout;
  592. ds = (void *)child->thread.ds_area_msr;
  593. }
  594. if (cfg.flags & PTRACE_BTS_O_SIGNAL)
  595. ret = ds_set_overflow(ds, DS_O_SIGNAL);
  596. else
  597. ret = ds_set_overflow(ds, DS_O_WRAP);
  598. if (ret < 0)
  599. goto errout;
  600. if (cfg.flags & PTRACE_BTS_O_TRACE)
  601. child->thread.debugctlmsr |= ds_debugctl_mask();
  602. else
  603. child->thread.debugctlmsr &= ~ds_debugctl_mask();
  604. if (cfg.flags & PTRACE_BTS_O_SCHED)
  605. set_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
  606. else
  607. clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
  608. ret = sizeof(cfg);
  609. out:
  610. if (child->thread.debugctlmsr)
  611. set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
  612. else
  613. clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
  614. return ret;
  615. errout:
  616. child->thread.debugctlmsr &= ~ds_debugctl_mask();
  617. clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
  618. goto out;
  619. }
  620. static int ptrace_bts_status(struct task_struct *child,
  621. long cfg_size,
  622. struct ptrace_bts_config __user *ucfg)
  623. {
  624. void *ds = (void *)child->thread.ds_area_msr;
  625. struct ptrace_bts_config cfg;
  626. if (cfg_size < sizeof(cfg))
  627. return -EIO;
  628. memset(&cfg, 0, sizeof(cfg));
  629. if (ds) {
  630. cfg.size = ds_get_bts_size(ds);
  631. if (ds_get_overflow(ds) == DS_O_SIGNAL)
  632. cfg.flags |= PTRACE_BTS_O_SIGNAL;
  633. if (test_tsk_thread_flag(child, TIF_DEBUGCTLMSR) &&
  634. child->thread.debugctlmsr & ds_debugctl_mask())
  635. cfg.flags |= PTRACE_BTS_O_TRACE;
  636. if (test_tsk_thread_flag(child, TIF_BTS_TRACE_TS))
  637. cfg.flags |= PTRACE_BTS_O_SCHED;
  638. }
  639. cfg.bts_size = sizeof(struct bts_struct);
  640. if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
  641. return -EFAULT;
  642. return sizeof(cfg);
  643. }
  644. static int ptrace_bts_write_record(struct task_struct *child,
  645. const struct bts_struct *in)
  646. {
  647. int retval;
  648. if (!child->thread.ds_area_msr)
  649. return -ENXIO;
  650. retval = ds_write_bts((void *)child->thread.ds_area_msr, in);
  651. if (retval)
  652. return retval;
  653. return sizeof(*in);
  654. }
  655. static int ptrace_bts_realloc(struct task_struct *child,
  656. int size, int reduce_size)
  657. {
  658. unsigned long rlim, vm;
  659. int ret, old_size;
  660. if (size < 0)
  661. return -EINVAL;
  662. old_size = ds_get_bts_size((void *)child->thread.ds_area_msr);
  663. if (old_size < 0)
  664. return old_size;
  665. ret = ds_free((void **)&child->thread.ds_area_msr);
  666. if (ret < 0)
  667. goto out;
  668. size >>= PAGE_SHIFT;
  669. old_size >>= PAGE_SHIFT;
  670. current->mm->total_vm -= old_size;
  671. current->mm->locked_vm -= old_size;
  672. if (size == 0)
  673. goto out;
  674. rlim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
  675. vm = current->mm->total_vm + size;
  676. if (rlim < vm) {
  677. ret = -ENOMEM;
  678. if (!reduce_size)
  679. goto out;
  680. size = rlim - current->mm->total_vm;
  681. if (size <= 0)
  682. goto out;
  683. }
  684. rlim = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
  685. vm = current->mm->locked_vm + size;
  686. if (rlim < vm) {
  687. ret = -ENOMEM;
  688. if (!reduce_size)
  689. goto out;
  690. size = rlim - current->mm->locked_vm;
  691. if (size <= 0)
  692. goto out;
  693. }
  694. ret = ds_allocate((void **)&child->thread.ds_area_msr,
  695. size << PAGE_SHIFT);
  696. if (ret < 0)
  697. goto out;
  698. current->mm->total_vm += size;
  699. current->mm->locked_vm += size;
  700. out:
  701. if (child->thread.ds_area_msr)
  702. set_tsk_thread_flag(child, TIF_DS_AREA_MSR);
  703. else
  704. clear_tsk_thread_flag(child, TIF_DS_AREA_MSR);
  705. return ret;
  706. }
  707. void ptrace_bts_take_timestamp(struct task_struct *tsk,
  708. enum bts_qualifier qualifier)
  709. {
  710. struct bts_struct rec = {
  711. .qualifier = qualifier,
  712. .variant.jiffies = jiffies_64
  713. };
  714. ptrace_bts_write_record(tsk, &rec);
  715. }
  716. #endif /* X86_BTS */
  717. /*
  718. * Called by kernel/ptrace.c when detaching..
  719. *
  720. * Make sure the single step bit is not set.
  721. */
  722. void ptrace_disable(struct task_struct *child)
  723. {
  724. user_disable_single_step(child);
  725. #ifdef TIF_SYSCALL_EMU
  726. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  727. #endif
  728. if (child->thread.ds_area_msr) {
  729. #ifdef X86_BTS
  730. ptrace_bts_realloc(child, 0, 0);
  731. #endif
  732. child->thread.debugctlmsr &= ~ds_debugctl_mask();
  733. if (!child->thread.debugctlmsr)
  734. clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
  735. clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
  736. }
  737. }
  738. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  739. static const struct user_regset_view user_x86_32_view; /* Initialized below. */
  740. #endif
  741. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  742. {
  743. int ret;
  744. unsigned long __user *datap = (unsigned long __user *)data;
  745. switch (request) {
  746. /* read the word at location addr in the USER area. */
  747. case PTRACE_PEEKUSR: {
  748. unsigned long tmp;
  749. ret = -EIO;
  750. if ((addr & (sizeof(data) - 1)) || addr < 0 ||
  751. addr >= sizeof(struct user))
  752. break;
  753. tmp = 0; /* Default return condition */
  754. if (addr < sizeof(struct user_regs_struct))
  755. tmp = getreg(child, addr);
  756. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  757. addr <= offsetof(struct user, u_debugreg[7])) {
  758. addr -= offsetof(struct user, u_debugreg[0]);
  759. tmp = ptrace_get_debugreg(child, addr / sizeof(data));
  760. }
  761. ret = put_user(tmp, datap);
  762. break;
  763. }
  764. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  765. ret = -EIO;
  766. if ((addr & (sizeof(data) - 1)) || addr < 0 ||
  767. addr >= sizeof(struct user))
  768. break;
  769. if (addr < sizeof(struct user_regs_struct))
  770. ret = putreg(child, addr, data);
  771. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  772. addr <= offsetof(struct user, u_debugreg[7])) {
  773. addr -= offsetof(struct user, u_debugreg[0]);
  774. ret = ptrace_set_debugreg(child,
  775. addr / sizeof(data), data);
  776. }
  777. break;
  778. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  779. return copy_regset_to_user(child,
  780. task_user_regset_view(current),
  781. REGSET_GENERAL,
  782. 0, sizeof(struct user_regs_struct),
  783. datap);
  784. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  785. return copy_regset_from_user(child,
  786. task_user_regset_view(current),
  787. REGSET_GENERAL,
  788. 0, sizeof(struct user_regs_struct),
  789. datap);
  790. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  791. return copy_regset_to_user(child,
  792. task_user_regset_view(current),
  793. REGSET_FP,
  794. 0, sizeof(struct user_i387_struct),
  795. datap);
  796. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  797. return copy_regset_from_user(child,
  798. task_user_regset_view(current),
  799. REGSET_FP,
  800. 0, sizeof(struct user_i387_struct),
  801. datap);
  802. #ifdef CONFIG_X86_32
  803. case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
  804. return copy_regset_to_user(child, &user_x86_32_view,
  805. REGSET_XFP,
  806. 0, sizeof(struct user_fxsr_struct),
  807. datap);
  808. case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
  809. return copy_regset_from_user(child, &user_x86_32_view,
  810. REGSET_XFP,
  811. 0, sizeof(struct user_fxsr_struct),
  812. datap);
  813. #endif
  814. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  815. case PTRACE_GET_THREAD_AREA:
  816. if (addr < 0)
  817. return -EIO;
  818. ret = do_get_thread_area(child, addr,
  819. (struct user_desc __user *) data);
  820. break;
  821. case PTRACE_SET_THREAD_AREA:
  822. if (addr < 0)
  823. return -EIO;
  824. ret = do_set_thread_area(child, addr,
  825. (struct user_desc __user *) data, 0);
  826. break;
  827. #endif
  828. #ifdef CONFIG_X86_64
  829. /* normal 64bit interface to access TLS data.
  830. Works just like arch_prctl, except that the arguments
  831. are reversed. */
  832. case PTRACE_ARCH_PRCTL:
  833. ret = do_arch_prctl(child, data, addr);
  834. break;
  835. #endif
  836. /*
  837. * These bits need more cooking - not enabled yet:
  838. */
  839. #ifdef X86_BTS
  840. case PTRACE_BTS_CONFIG:
  841. ret = ptrace_bts_config
  842. (child, data, (struct ptrace_bts_config __user *)addr);
  843. break;
  844. case PTRACE_BTS_STATUS:
  845. ret = ptrace_bts_status
  846. (child, data, (struct ptrace_bts_config __user *)addr);
  847. break;
  848. case PTRACE_BTS_SIZE:
  849. ret = ptrace_bts_get_size(child);
  850. break;
  851. case PTRACE_BTS_GET:
  852. ret = ptrace_bts_read_record
  853. (child, data, (struct bts_struct __user *) addr);
  854. break;
  855. case PTRACE_BTS_CLEAR:
  856. ret = ptrace_bts_clear(child);
  857. break;
  858. case PTRACE_BTS_DRAIN:
  859. ret = ptrace_bts_drain
  860. (child, data, (struct bts_struct __user *) addr);
  861. break;
  862. #endif
  863. default:
  864. ret = ptrace_request(child, request, addr, data);
  865. break;
  866. }
  867. return ret;
  868. }
  869. #ifdef CONFIG_IA32_EMULATION
  870. #include <linux/compat.h>
  871. #include <linux/syscalls.h>
  872. #include <asm/ia32.h>
  873. #include <asm/user32.h>
  874. #define R32(l,q) \
  875. case offsetof(struct user32, regs.l): \
  876. regs->q = value; break
  877. #define SEG32(rs) \
  878. case offsetof(struct user32, regs.rs): \
  879. return set_segment_reg(child, \
  880. offsetof(struct user_regs_struct, rs), \
  881. value); \
  882. break
  883. static int putreg32(struct task_struct *child, unsigned regno, u32 value)
  884. {
  885. struct pt_regs *regs = task_pt_regs(child);
  886. switch (regno) {
  887. SEG32(cs);
  888. SEG32(ds);
  889. SEG32(es);
  890. SEG32(fs);
  891. SEG32(gs);
  892. SEG32(ss);
  893. R32(ebx, bx);
  894. R32(ecx, cx);
  895. R32(edx, dx);
  896. R32(edi, di);
  897. R32(esi, si);
  898. R32(ebp, bp);
  899. R32(eax, ax);
  900. R32(eip, ip);
  901. R32(esp, sp);
  902. case offsetof(struct user32, regs.orig_eax):
  903. /*
  904. * Sign-extend the value so that orig_eax = -1
  905. * causes (long)orig_ax < 0 tests to fire correctly.
  906. */
  907. regs->orig_ax = (long) (s32) value;
  908. break;
  909. case offsetof(struct user32, regs.eflags):
  910. return set_flags(child, value);
  911. case offsetof(struct user32, u_debugreg[0]) ...
  912. offsetof(struct user32, u_debugreg[7]):
  913. regno -= offsetof(struct user32, u_debugreg[0]);
  914. return ptrace_set_debugreg(child, regno / 4, value);
  915. default:
  916. if (regno > sizeof(struct user32) || (regno & 3))
  917. return -EIO;
  918. /*
  919. * Other dummy fields in the virtual user structure
  920. * are ignored
  921. */
  922. break;
  923. }
  924. return 0;
  925. }
  926. #undef R32
  927. #undef SEG32
  928. #define R32(l,q) \
  929. case offsetof(struct user32, regs.l): \
  930. *val = regs->q; break
  931. #define SEG32(rs) \
  932. case offsetof(struct user32, regs.rs): \
  933. *val = get_segment_reg(child, \
  934. offsetof(struct user_regs_struct, rs)); \
  935. break
  936. static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
  937. {
  938. struct pt_regs *regs = task_pt_regs(child);
  939. switch (regno) {
  940. SEG32(ds);
  941. SEG32(es);
  942. SEG32(fs);
  943. SEG32(gs);
  944. R32(cs, cs);
  945. R32(ss, ss);
  946. R32(ebx, bx);
  947. R32(ecx, cx);
  948. R32(edx, dx);
  949. R32(edi, di);
  950. R32(esi, si);
  951. R32(ebp, bp);
  952. R32(eax, ax);
  953. R32(orig_eax, orig_ax);
  954. R32(eip, ip);
  955. R32(esp, sp);
  956. case offsetof(struct user32, regs.eflags):
  957. *val = get_flags(child);
  958. break;
  959. case offsetof(struct user32, u_debugreg[0]) ...
  960. offsetof(struct user32, u_debugreg[7]):
  961. regno -= offsetof(struct user32, u_debugreg[0]);
  962. *val = ptrace_get_debugreg(child, regno / 4);
  963. break;
  964. default:
  965. if (regno > sizeof(struct user32) || (regno & 3))
  966. return -EIO;
  967. /*
  968. * Other dummy fields in the virtual user structure
  969. * are ignored
  970. */
  971. *val = 0;
  972. break;
  973. }
  974. return 0;
  975. }
  976. #undef R32
  977. #undef SEG32
  978. static int genregs32_get(struct task_struct *target,
  979. const struct user_regset *regset,
  980. unsigned int pos, unsigned int count,
  981. void *kbuf, void __user *ubuf)
  982. {
  983. if (kbuf) {
  984. compat_ulong_t *k = kbuf;
  985. while (count > 0) {
  986. getreg32(target, pos, k++);
  987. count -= sizeof(*k);
  988. pos += sizeof(*k);
  989. }
  990. } else {
  991. compat_ulong_t __user *u = ubuf;
  992. while (count > 0) {
  993. compat_ulong_t word;
  994. getreg32(target, pos, &word);
  995. if (__put_user(word, u++))
  996. return -EFAULT;
  997. count -= sizeof(*u);
  998. pos += sizeof(*u);
  999. }
  1000. }
  1001. return 0;
  1002. }
  1003. static int genregs32_set(struct task_struct *target,
  1004. const struct user_regset *regset,
  1005. unsigned int pos, unsigned int count,
  1006. const void *kbuf, const void __user *ubuf)
  1007. {
  1008. int ret = 0;
  1009. if (kbuf) {
  1010. const compat_ulong_t *k = kbuf;
  1011. while (count > 0 && !ret) {
  1012. ret = putreg32(target, pos, *k++);
  1013. count -= sizeof(*k);
  1014. pos += sizeof(*k);
  1015. }
  1016. } else {
  1017. const compat_ulong_t __user *u = ubuf;
  1018. while (count > 0 && !ret) {
  1019. compat_ulong_t word;
  1020. ret = __get_user(word, u++);
  1021. if (ret)
  1022. break;
  1023. ret = putreg32(target, pos, word);
  1024. count -= sizeof(*u);
  1025. pos += sizeof(*u);
  1026. }
  1027. }
  1028. return ret;
  1029. }
  1030. static long ptrace32_siginfo(unsigned request, u32 pid, u32 addr, u32 data)
  1031. {
  1032. siginfo_t __user *si = compat_alloc_user_space(sizeof(siginfo_t));
  1033. compat_siginfo_t __user *si32 = compat_ptr(data);
  1034. siginfo_t ssi;
  1035. int ret;
  1036. if (request == PTRACE_SETSIGINFO) {
  1037. memset(&ssi, 0, sizeof(siginfo_t));
  1038. ret = copy_siginfo_from_user32(&ssi, si32);
  1039. if (ret)
  1040. return ret;
  1041. if (copy_to_user(si, &ssi, sizeof(siginfo_t)))
  1042. return -EFAULT;
  1043. }
  1044. ret = sys_ptrace(request, pid, addr, (unsigned long)si);
  1045. if (ret)
  1046. return ret;
  1047. if (request == PTRACE_GETSIGINFO) {
  1048. if (copy_from_user(&ssi, si, sizeof(siginfo_t)))
  1049. return -EFAULT;
  1050. ret = copy_siginfo_to_user32(si32, &ssi);
  1051. }
  1052. return ret;
  1053. }
  1054. asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
  1055. {
  1056. struct task_struct *child;
  1057. struct pt_regs *childregs;
  1058. void __user *datap = compat_ptr(data);
  1059. int ret;
  1060. __u32 val;
  1061. switch (request) {
  1062. case PTRACE_TRACEME:
  1063. case PTRACE_ATTACH:
  1064. case PTRACE_KILL:
  1065. case PTRACE_CONT:
  1066. case PTRACE_SINGLESTEP:
  1067. case PTRACE_SINGLEBLOCK:
  1068. case PTRACE_DETACH:
  1069. case PTRACE_SYSCALL:
  1070. case PTRACE_OLDSETOPTIONS:
  1071. case PTRACE_SETOPTIONS:
  1072. case PTRACE_SET_THREAD_AREA:
  1073. case PTRACE_GET_THREAD_AREA:
  1074. #ifdef X86_BTS
  1075. case PTRACE_BTS_CONFIG:
  1076. case PTRACE_BTS_STATUS:
  1077. case PTRACE_BTS_SIZE:
  1078. case PTRACE_BTS_GET:
  1079. case PTRACE_BTS_CLEAR:
  1080. case PTRACE_BTS_DRAIN:
  1081. #endif
  1082. return sys_ptrace(request, pid, addr, data);
  1083. default:
  1084. return -EINVAL;
  1085. case PTRACE_PEEKTEXT:
  1086. case PTRACE_PEEKDATA:
  1087. case PTRACE_POKEDATA:
  1088. case PTRACE_POKETEXT:
  1089. case PTRACE_POKEUSR:
  1090. case PTRACE_PEEKUSR:
  1091. case PTRACE_GETREGS:
  1092. case PTRACE_SETREGS:
  1093. case PTRACE_SETFPREGS:
  1094. case PTRACE_GETFPREGS:
  1095. case PTRACE_SETFPXREGS:
  1096. case PTRACE_GETFPXREGS:
  1097. case PTRACE_GETEVENTMSG:
  1098. break;
  1099. case PTRACE_SETSIGINFO:
  1100. case PTRACE_GETSIGINFO:
  1101. return ptrace32_siginfo(request, pid, addr, data);
  1102. }
  1103. child = ptrace_get_task_struct(pid);
  1104. if (IS_ERR(child))
  1105. return PTR_ERR(child);
  1106. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  1107. if (ret < 0)
  1108. goto out;
  1109. childregs = task_pt_regs(child);
  1110. switch (request) {
  1111. case PTRACE_PEEKUSR:
  1112. ret = getreg32(child, addr, &val);
  1113. if (ret == 0)
  1114. ret = put_user(val, (__u32 __user *)datap);
  1115. break;
  1116. case PTRACE_POKEUSR:
  1117. ret = putreg32(child, addr, data);
  1118. break;
  1119. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  1120. return copy_regset_to_user(child, &user_x86_32_view,
  1121. REGSET_GENERAL,
  1122. 0, sizeof(struct user_regs_struct32),
  1123. datap);
  1124. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  1125. return copy_regset_from_user(child, &user_x86_32_view,
  1126. REGSET_GENERAL, 0,
  1127. sizeof(struct user_regs_struct32),
  1128. datap);
  1129. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  1130. return copy_regset_to_user(child, &user_x86_32_view,
  1131. REGSET_FP, 0,
  1132. sizeof(struct user_i387_ia32_struct),
  1133. datap);
  1134. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  1135. return copy_regset_from_user(
  1136. child, &user_x86_32_view, REGSET_FP,
  1137. 0, sizeof(struct user_i387_ia32_struct), datap);
  1138. case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
  1139. return copy_regset_to_user(child, &user_x86_32_view,
  1140. REGSET_XFP, 0,
  1141. sizeof(struct user32_fxsr_struct),
  1142. datap);
  1143. case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
  1144. return copy_regset_from_user(child, &user_x86_32_view,
  1145. REGSET_XFP, 0,
  1146. sizeof(struct user32_fxsr_struct),
  1147. datap);
  1148. default:
  1149. return compat_ptrace_request(child, request, addr, data);
  1150. }
  1151. out:
  1152. put_task_struct(child);
  1153. return ret;
  1154. }
  1155. #endif /* CONFIG_IA32_EMULATION */
  1156. #ifdef CONFIG_X86_64
  1157. static const struct user_regset x86_64_regsets[] = {
  1158. [REGSET_GENERAL] = {
  1159. .core_note_type = NT_PRSTATUS,
  1160. .n = sizeof(struct user_regs_struct) / sizeof(long),
  1161. .size = sizeof(long), .align = sizeof(long),
  1162. .get = genregs_get, .set = genregs_set
  1163. },
  1164. [REGSET_FP] = {
  1165. .core_note_type = NT_PRFPREG,
  1166. .n = sizeof(struct user_i387_struct) / sizeof(long),
  1167. .size = sizeof(long), .align = sizeof(long),
  1168. .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
  1169. },
  1170. };
  1171. static const struct user_regset_view user_x86_64_view = {
  1172. .name = "x86_64", .e_machine = EM_X86_64,
  1173. .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
  1174. };
  1175. #else /* CONFIG_X86_32 */
  1176. #define user_regs_struct32 user_regs_struct
  1177. #define genregs32_get genregs_get
  1178. #define genregs32_set genregs_set
  1179. #endif /* CONFIG_X86_64 */
  1180. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1181. static const struct user_regset x86_32_regsets[] = {
  1182. [REGSET_GENERAL] = {
  1183. .core_note_type = NT_PRSTATUS,
  1184. .n = sizeof(struct user_regs_struct32) / sizeof(u32),
  1185. .size = sizeof(u32), .align = sizeof(u32),
  1186. .get = genregs32_get, .set = genregs32_set
  1187. },
  1188. [REGSET_FP] = {
  1189. .core_note_type = NT_PRFPREG,
  1190. .n = sizeof(struct user_i387_struct) / sizeof(u32),
  1191. .size = sizeof(u32), .align = sizeof(u32),
  1192. .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
  1193. },
  1194. [REGSET_XFP] = {
  1195. .core_note_type = NT_PRXFPREG,
  1196. .n = sizeof(struct user_i387_struct) / sizeof(u32),
  1197. .size = sizeof(u32), .align = sizeof(u32),
  1198. .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
  1199. },
  1200. [REGSET_TLS] = {
  1201. .core_note_type = NT_386_TLS,
  1202. .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
  1203. .size = sizeof(struct user_desc),
  1204. .align = sizeof(struct user_desc),
  1205. .active = regset_tls_active,
  1206. .get = regset_tls_get, .set = regset_tls_set
  1207. },
  1208. };
  1209. static const struct user_regset_view user_x86_32_view = {
  1210. .name = "i386", .e_machine = EM_386,
  1211. .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
  1212. };
  1213. #endif
  1214. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  1215. {
  1216. #ifdef CONFIG_IA32_EMULATION
  1217. if (test_tsk_thread_flag(task, TIF_IA32))
  1218. #endif
  1219. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1220. return &user_x86_32_view;
  1221. #endif
  1222. #ifdef CONFIG_X86_64
  1223. return &user_x86_64_view;
  1224. #endif
  1225. }
  1226. #ifdef CONFIG_X86_32
  1227. void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)
  1228. {
  1229. struct siginfo info;
  1230. tsk->thread.trap_no = 1;
  1231. tsk->thread.error_code = error_code;
  1232. memset(&info, 0, sizeof(info));
  1233. info.si_signo = SIGTRAP;
  1234. info.si_code = TRAP_BRKPT;
  1235. /* User-mode ip? */
  1236. info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
  1237. /* Send us the fake SIGTRAP */
  1238. force_sig_info(SIGTRAP, &info, tsk);
  1239. }
  1240. /* notification of system call entry/exit
  1241. * - triggered by current->work.syscall_trace
  1242. */
  1243. int do_syscall_trace(struct pt_regs *regs, int entryexit)
  1244. {
  1245. int is_sysemu = test_thread_flag(TIF_SYSCALL_EMU);
  1246. /*
  1247. * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall
  1248. * interception
  1249. */
  1250. int is_singlestep = !is_sysemu && test_thread_flag(TIF_SINGLESTEP);
  1251. int ret = 0;
  1252. /* do the secure computing check first */
  1253. if (!entryexit)
  1254. secure_computing(regs->orig_ax);
  1255. if (unlikely(current->audit_context)) {
  1256. if (entryexit)
  1257. audit_syscall_exit(AUDITSC_RESULT(regs->ax),
  1258. regs->ax);
  1259. /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only
  1260. * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is
  1261. * not used, entry.S will call us only on syscall exit, not
  1262. * entry; so when TIF_SYSCALL_AUDIT is used we must avoid
  1263. * calling send_sigtrap() on syscall entry.
  1264. *
  1265. * Note that when PTRACE_SYSEMU_SINGLESTEP is used,
  1266. * is_singlestep is false, despite his name, so we will still do
  1267. * the correct thing.
  1268. */
  1269. else if (is_singlestep)
  1270. goto out;
  1271. }
  1272. if (!(current->ptrace & PT_PTRACED))
  1273. goto out;
  1274. /* If a process stops on the 1st tracepoint with SYSCALL_TRACE
  1275. * and then is resumed with SYSEMU_SINGLESTEP, it will come in
  1276. * here. We have to check this and return */
  1277. if (is_sysemu && entryexit)
  1278. return 0;
  1279. /* Fake a debug trap */
  1280. if (is_singlestep)
  1281. send_sigtrap(current, regs, 0);
  1282. if (!test_thread_flag(TIF_SYSCALL_TRACE) && !is_sysemu)
  1283. goto out;
  1284. /* the 0x80 provides a way for the tracing parent to distinguish
  1285. between a syscall stop and SIGTRAP delivery */
  1286. /* Note that the debugger could change the result of test_thread_flag!*/
  1287. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80:0));
  1288. /*
  1289. * this isn't the same as continuing with a signal, but it will do
  1290. * for normal use. strace only continues with a signal if the
  1291. * stopping signal is not SIGTRAP. -brl
  1292. */
  1293. if (current->exit_code) {
  1294. send_sig(current->exit_code, current, 1);
  1295. current->exit_code = 0;
  1296. }
  1297. ret = is_sysemu;
  1298. out:
  1299. if (unlikely(current->audit_context) && !entryexit)
  1300. audit_syscall_entry(AUDIT_ARCH_I386, regs->orig_ax,
  1301. regs->bx, regs->cx, regs->dx, regs->si);
  1302. if (ret == 0)
  1303. return 0;
  1304. regs->orig_ax = -1; /* force skip of syscall restarting */
  1305. if (unlikely(current->audit_context))
  1306. audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
  1307. return 1;
  1308. }
  1309. #else /* CONFIG_X86_64 */
  1310. static void syscall_trace(struct pt_regs *regs)
  1311. {
  1312. #if 0
  1313. printk("trace %s ip %lx sp %lx ax %d origrax %d caller %lx tiflags %x ptrace %x\n",
  1314. current->comm,
  1315. regs->ip, regs->sp, regs->ax, regs->orig_ax, __builtin_return_address(0),
  1316. current_thread_info()->flags, current->ptrace);
  1317. #endif
  1318. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  1319. ? 0x80 : 0));
  1320. /*
  1321. * this isn't the same as continuing with a signal, but it will do
  1322. * for normal use. strace only continues with a signal if the
  1323. * stopping signal is not SIGTRAP. -brl
  1324. */
  1325. if (current->exit_code) {
  1326. send_sig(current->exit_code, current, 1);
  1327. current->exit_code = 0;
  1328. }
  1329. }
  1330. asmlinkage void syscall_trace_enter(struct pt_regs *regs)
  1331. {
  1332. /* do the secure computing check first */
  1333. secure_computing(regs->orig_ax);
  1334. if (test_thread_flag(TIF_SYSCALL_TRACE)
  1335. && (current->ptrace & PT_PTRACED))
  1336. syscall_trace(regs);
  1337. if (unlikely(current->audit_context)) {
  1338. if (test_thread_flag(TIF_IA32)) {
  1339. audit_syscall_entry(AUDIT_ARCH_I386,
  1340. regs->orig_ax,
  1341. regs->bx, regs->cx,
  1342. regs->dx, regs->si);
  1343. } else {
  1344. audit_syscall_entry(AUDIT_ARCH_X86_64,
  1345. regs->orig_ax,
  1346. regs->di, regs->si,
  1347. regs->dx, regs->r10);
  1348. }
  1349. }
  1350. }
  1351. asmlinkage void syscall_trace_leave(struct pt_regs *regs)
  1352. {
  1353. if (unlikely(current->audit_context))
  1354. audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
  1355. if ((test_thread_flag(TIF_SYSCALL_TRACE)
  1356. || test_thread_flag(TIF_SINGLESTEP))
  1357. && (current->ptrace & PT_PTRACED))
  1358. syscall_trace(regs);
  1359. }
  1360. #endif /* CONFIG_X86_32 */