ptrace.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526
  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. if (offset != offsetof(struct user_regs_struct, gs))
  94. *pt_regs_access(task_pt_regs(task), offset) = value;
  95. else {
  96. task->thread.gs = value;
  97. if (task == current)
  98. /*
  99. * The user-mode %gs is not affected by
  100. * kernel entry, so we must update the CPU.
  101. */
  102. loadsegment(gs, value);
  103. }
  104. return 0;
  105. }
  106. static unsigned long debugreg_addr_limit(struct task_struct *task)
  107. {
  108. return TASK_SIZE - 3;
  109. }
  110. #else /* CONFIG_X86_64 */
  111. #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
  112. static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
  113. {
  114. BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
  115. return &regs->r15 + (offset / sizeof(regs->r15));
  116. }
  117. static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
  118. {
  119. /*
  120. * Returning the value truncates it to 16 bits.
  121. */
  122. unsigned int seg;
  123. switch (offset) {
  124. case offsetof(struct user_regs_struct, fs):
  125. if (task == current) {
  126. /* Older gas can't assemble movq %?s,%r?? */
  127. asm("movl %%fs,%0" : "=r" (seg));
  128. return seg;
  129. }
  130. return task->thread.fsindex;
  131. case offsetof(struct user_regs_struct, gs):
  132. if (task == current) {
  133. asm("movl %%gs,%0" : "=r" (seg));
  134. return seg;
  135. }
  136. return task->thread.gsindex;
  137. case offsetof(struct user_regs_struct, ds):
  138. if (task == current) {
  139. asm("movl %%ds,%0" : "=r" (seg));
  140. return seg;
  141. }
  142. return task->thread.ds;
  143. case offsetof(struct user_regs_struct, es):
  144. if (task == current) {
  145. asm("movl %%es,%0" : "=r" (seg));
  146. return seg;
  147. }
  148. return task->thread.es;
  149. case offsetof(struct user_regs_struct, cs):
  150. case offsetof(struct user_regs_struct, ss):
  151. break;
  152. }
  153. return *pt_regs_access(task_pt_regs(task), offset);
  154. }
  155. static int set_segment_reg(struct task_struct *task,
  156. unsigned long offset, u16 value)
  157. {
  158. /*
  159. * The value argument was already truncated to 16 bits.
  160. */
  161. if (invalid_selector(value))
  162. return -EIO;
  163. switch (offset) {
  164. case offsetof(struct user_regs_struct,fs):
  165. /*
  166. * If this is setting fs as for normal 64-bit use but
  167. * setting fs_base has implicitly changed it, leave it.
  168. */
  169. if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
  170. task->thread.fs != 0) ||
  171. (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
  172. task->thread.fs == 0))
  173. break;
  174. task->thread.fsindex = value;
  175. if (task == current)
  176. loadsegment(fs, task->thread.fsindex);
  177. break;
  178. case offsetof(struct user_regs_struct,gs):
  179. /*
  180. * If this is setting gs as for normal 64-bit use but
  181. * setting gs_base has implicitly changed it, leave it.
  182. */
  183. if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
  184. task->thread.gs != 0) ||
  185. (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
  186. task->thread.gs == 0))
  187. break;
  188. task->thread.gsindex = value;
  189. if (task == current)
  190. load_gs_index(task->thread.gsindex);
  191. break;
  192. case offsetof(struct user_regs_struct,ds):
  193. task->thread.ds = value;
  194. if (task == current)
  195. loadsegment(ds, task->thread.ds);
  196. break;
  197. case offsetof(struct user_regs_struct,es):
  198. task->thread.es = value;
  199. if (task == current)
  200. loadsegment(es, task->thread.es);
  201. break;
  202. /*
  203. * Can't actually change these in 64-bit mode.
  204. */
  205. case offsetof(struct user_regs_struct,cs):
  206. #ifdef CONFIG_IA32_EMULATION
  207. if (test_tsk_thread_flag(task, TIF_IA32))
  208. task_pt_regs(task)->cs = value;
  209. #endif
  210. break;
  211. case offsetof(struct user_regs_struct,ss):
  212. #ifdef CONFIG_IA32_EMULATION
  213. if (test_tsk_thread_flag(task, TIF_IA32))
  214. task_pt_regs(task)->ss = value;
  215. #endif
  216. break;
  217. }
  218. return 0;
  219. }
  220. static unsigned long debugreg_addr_limit(struct task_struct *task)
  221. {
  222. #ifdef CONFIG_IA32_EMULATION
  223. if (test_tsk_thread_flag(task, TIF_IA32))
  224. return IA32_PAGE_OFFSET - 3;
  225. #endif
  226. return TASK_SIZE64 - 7;
  227. }
  228. #endif /* CONFIG_X86_32 */
  229. static unsigned long get_flags(struct task_struct *task)
  230. {
  231. unsigned long retval = task_pt_regs(task)->flags;
  232. /*
  233. * If the debugger set TF, hide it from the readout.
  234. */
  235. if (test_tsk_thread_flag(task, TIF_FORCED_TF))
  236. retval &= ~X86_EFLAGS_TF;
  237. return retval;
  238. }
  239. static int set_flags(struct task_struct *task, unsigned long value)
  240. {
  241. struct pt_regs *regs = task_pt_regs(task);
  242. /*
  243. * If the user value contains TF, mark that
  244. * it was not "us" (the debugger) that set it.
  245. * If not, make sure it stays set if we had.
  246. */
  247. if (value & X86_EFLAGS_TF)
  248. clear_tsk_thread_flag(task, TIF_FORCED_TF);
  249. else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
  250. value |= X86_EFLAGS_TF;
  251. regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
  252. return 0;
  253. }
  254. static int putreg(struct task_struct *child,
  255. unsigned long offset, unsigned long value)
  256. {
  257. switch (offset) {
  258. case offsetof(struct user_regs_struct, cs):
  259. case offsetof(struct user_regs_struct, ds):
  260. case offsetof(struct user_regs_struct, es):
  261. case offsetof(struct user_regs_struct, fs):
  262. case offsetof(struct user_regs_struct, gs):
  263. case offsetof(struct user_regs_struct, ss):
  264. return set_segment_reg(child, offset, value);
  265. case offsetof(struct user_regs_struct, flags):
  266. return set_flags(child, value);
  267. #ifdef CONFIG_X86_64
  268. case offsetof(struct user_regs_struct,fs_base):
  269. if (value >= TASK_SIZE_OF(child))
  270. return -EIO;
  271. /*
  272. * When changing the segment base, use do_arch_prctl
  273. * to set either thread.fs or thread.fsindex and the
  274. * corresponding GDT slot.
  275. */
  276. if (child->thread.fs != value)
  277. return do_arch_prctl(child, ARCH_SET_FS, value);
  278. return 0;
  279. case offsetof(struct user_regs_struct,gs_base):
  280. /*
  281. * Exactly the same here as the %fs handling above.
  282. */
  283. if (value >= TASK_SIZE_OF(child))
  284. return -EIO;
  285. if (child->thread.gs != value)
  286. return do_arch_prctl(child, ARCH_SET_GS, value);
  287. return 0;
  288. #endif
  289. }
  290. *pt_regs_access(task_pt_regs(child), offset) = value;
  291. return 0;
  292. }
  293. static unsigned long getreg(struct task_struct *task, unsigned long offset)
  294. {
  295. switch (offset) {
  296. case offsetof(struct user_regs_struct, cs):
  297. case offsetof(struct user_regs_struct, ds):
  298. case offsetof(struct user_regs_struct, es):
  299. case offsetof(struct user_regs_struct, fs):
  300. case offsetof(struct user_regs_struct, gs):
  301. case offsetof(struct user_regs_struct, ss):
  302. return get_segment_reg(task, offset);
  303. case offsetof(struct user_regs_struct, flags):
  304. return get_flags(task);
  305. #ifdef CONFIG_X86_64
  306. case offsetof(struct user_regs_struct, fs_base): {
  307. /*
  308. * do_arch_prctl may have used a GDT slot instead of
  309. * the MSR. To userland, it appears the same either
  310. * way, except the %fs segment selector might not be 0.
  311. */
  312. unsigned int seg = task->thread.fsindex;
  313. if (task->thread.fs != 0)
  314. return task->thread.fs;
  315. if (task == current)
  316. asm("movl %%fs,%0" : "=r" (seg));
  317. if (seg != FS_TLS_SEL)
  318. return 0;
  319. return get_desc_base(&task->thread.tls_array[FS_TLS]);
  320. }
  321. case offsetof(struct user_regs_struct, gs_base): {
  322. /*
  323. * Exactly the same here as the %fs handling above.
  324. */
  325. unsigned int seg = task->thread.gsindex;
  326. if (task->thread.gs != 0)
  327. return task->thread.gs;
  328. if (task == current)
  329. asm("movl %%gs,%0" : "=r" (seg));
  330. if (seg != GS_TLS_SEL)
  331. return 0;
  332. return get_desc_base(&task->thread.tls_array[GS_TLS]);
  333. }
  334. #endif
  335. }
  336. return *pt_regs_access(task_pt_regs(task), offset);
  337. }
  338. static int genregs_get(struct task_struct *target,
  339. const struct user_regset *regset,
  340. unsigned int pos, unsigned int count,
  341. void *kbuf, void __user *ubuf)
  342. {
  343. if (kbuf) {
  344. unsigned long *k = kbuf;
  345. while (count > 0) {
  346. *k++ = getreg(target, pos);
  347. count -= sizeof(*k);
  348. pos += sizeof(*k);
  349. }
  350. } else {
  351. unsigned long __user *u = ubuf;
  352. while (count > 0) {
  353. if (__put_user(getreg(target, pos), u++))
  354. return -EFAULT;
  355. count -= sizeof(*u);
  356. pos += sizeof(*u);
  357. }
  358. }
  359. return 0;
  360. }
  361. static int genregs_set(struct task_struct *target,
  362. const struct user_regset *regset,
  363. unsigned int pos, unsigned int count,
  364. const void *kbuf, const void __user *ubuf)
  365. {
  366. int ret = 0;
  367. if (kbuf) {
  368. const unsigned long *k = kbuf;
  369. while (count > 0 && !ret) {
  370. ret = putreg(target, pos, *k++);
  371. count -= sizeof(*k);
  372. pos += sizeof(*k);
  373. }
  374. } else {
  375. const unsigned long __user *u = ubuf;
  376. while (count > 0 && !ret) {
  377. unsigned long word;
  378. ret = __get_user(word, u++);
  379. if (ret)
  380. break;
  381. ret = putreg(target, pos, word);
  382. count -= sizeof(*u);
  383. pos += sizeof(*u);
  384. }
  385. }
  386. return ret;
  387. }
  388. /*
  389. * This function is trivial and will be inlined by the compiler.
  390. * Having it separates the implementation details of debug
  391. * registers from the interface details of ptrace.
  392. */
  393. static unsigned long ptrace_get_debugreg(struct task_struct *child, int n)
  394. {
  395. switch (n) {
  396. case 0: return child->thread.debugreg0;
  397. case 1: return child->thread.debugreg1;
  398. case 2: return child->thread.debugreg2;
  399. case 3: return child->thread.debugreg3;
  400. case 6: return child->thread.debugreg6;
  401. case 7: return child->thread.debugreg7;
  402. }
  403. return 0;
  404. }
  405. static int ptrace_set_debugreg(struct task_struct *child,
  406. int n, unsigned long data)
  407. {
  408. int i;
  409. if (unlikely(n == 4 || n == 5))
  410. return -EIO;
  411. if (n < 4 && unlikely(data >= debugreg_addr_limit(child)))
  412. return -EIO;
  413. switch (n) {
  414. case 0: child->thread.debugreg0 = data; break;
  415. case 1: child->thread.debugreg1 = data; break;
  416. case 2: child->thread.debugreg2 = data; break;
  417. case 3: child->thread.debugreg3 = data; break;
  418. case 6:
  419. if ((data & ~0xffffffffUL) != 0)
  420. return -EIO;
  421. child->thread.debugreg6 = data;
  422. break;
  423. case 7:
  424. /*
  425. * Sanity-check data. Take one half-byte at once with
  426. * check = (val >> (16 + 4*i)) & 0xf. It contains the
  427. * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
  428. * 2 and 3 are LENi. Given a list of invalid values,
  429. * we do mask |= 1 << invalid_value, so that
  430. * (mask >> check) & 1 is a correct test for invalid
  431. * values.
  432. *
  433. * R/Wi contains the type of the breakpoint /
  434. * watchpoint, LENi contains the length of the watched
  435. * data in the watchpoint case.
  436. *
  437. * The invalid values are:
  438. * - LENi == 0x10 (undefined), so mask |= 0x0f00. [32-bit]
  439. * - R/Wi == 0x10 (break on I/O reads or writes), so
  440. * mask |= 0x4444.
  441. * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
  442. * 0x1110.
  443. *
  444. * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
  445. *
  446. * See the Intel Manual "System Programming Guide",
  447. * 15.2.4
  448. *
  449. * Note that LENi == 0x10 is defined on x86_64 in long
  450. * mode (i.e. even for 32-bit userspace software, but
  451. * 64-bit kernel), so the x86_64 mask value is 0x5454.
  452. * See the AMD manual no. 24593 (AMD64 System Programming)
  453. */
  454. #ifdef CONFIG_X86_32
  455. #define DR7_MASK 0x5f54
  456. #else
  457. #define DR7_MASK 0x5554
  458. #endif
  459. data &= ~DR_CONTROL_RESERVED;
  460. for (i = 0; i < 4; i++)
  461. if ((DR7_MASK >> ((data >> (16 + 4*i)) & 0xf)) & 1)
  462. return -EIO;
  463. child->thread.debugreg7 = data;
  464. if (data)
  465. set_tsk_thread_flag(child, TIF_DEBUG);
  466. else
  467. clear_tsk_thread_flag(child, TIF_DEBUG);
  468. break;
  469. }
  470. return 0;
  471. }
  472. static int ptrace_bts_get_size(struct task_struct *child)
  473. {
  474. if (!child->thread.ds_area_msr)
  475. return -ENXIO;
  476. return ds_get_bts_index((void *)child->thread.ds_area_msr);
  477. }
  478. static int ptrace_bts_read_record(struct task_struct *child,
  479. long index,
  480. struct bts_struct __user *out)
  481. {
  482. struct bts_struct ret;
  483. int retval;
  484. int bts_end;
  485. int bts_index;
  486. if (!child->thread.ds_area_msr)
  487. return -ENXIO;
  488. if (index < 0)
  489. return -EINVAL;
  490. bts_end = ds_get_bts_end((void *)child->thread.ds_area_msr);
  491. if (bts_end <= index)
  492. return -EINVAL;
  493. /* translate the ptrace bts index into the ds bts index */
  494. bts_index = ds_get_bts_index((void *)child->thread.ds_area_msr);
  495. bts_index -= (index + 1);
  496. if (bts_index < 0)
  497. bts_index += bts_end;
  498. retval = ds_read_bts((void *)child->thread.ds_area_msr,
  499. bts_index, &ret);
  500. if (retval < 0)
  501. return retval;
  502. if (copy_to_user(out, &ret, sizeof(ret)))
  503. return -EFAULT;
  504. return sizeof(ret);
  505. }
  506. static int ptrace_bts_write_record(struct task_struct *child,
  507. const struct bts_struct *in)
  508. {
  509. int retval;
  510. if (!child->thread.ds_area_msr)
  511. return -ENXIO;
  512. retval = ds_write_bts((void *)child->thread.ds_area_msr, in);
  513. if (retval)
  514. return retval;
  515. return sizeof(*in);
  516. }
  517. static int ptrace_bts_clear(struct task_struct *child)
  518. {
  519. if (!child->thread.ds_area_msr)
  520. return -ENXIO;
  521. return ds_clear((void *)child->thread.ds_area_msr);
  522. }
  523. static int ptrace_bts_drain(struct task_struct *child,
  524. struct bts_struct __user *out)
  525. {
  526. int end, i;
  527. void *ds = (void *)child->thread.ds_area_msr;
  528. if (!ds)
  529. return -ENXIO;
  530. end = ds_get_bts_index(ds);
  531. if (end <= 0)
  532. return end;
  533. for (i = 0; i < end; i++, out++) {
  534. struct bts_struct ret;
  535. int retval;
  536. retval = ds_read_bts(ds, i, &ret);
  537. if (retval < 0)
  538. return retval;
  539. if (copy_to_user(out, &ret, sizeof(ret)))
  540. return -EFAULT;
  541. }
  542. ds_clear(ds);
  543. return i;
  544. }
  545. static int ptrace_bts_realloc(struct task_struct *child,
  546. int size, int reduce_size)
  547. {
  548. unsigned long rlim, vm;
  549. int ret, old_size;
  550. if (size < 0)
  551. return -EINVAL;
  552. old_size = ds_get_bts_size((void *)child->thread.ds_area_msr);
  553. if (old_size < 0)
  554. return old_size;
  555. ret = ds_free((void **)&child->thread.ds_area_msr);
  556. if (ret < 0)
  557. goto out;
  558. size >>= PAGE_SHIFT;
  559. old_size >>= PAGE_SHIFT;
  560. current->mm->total_vm -= old_size;
  561. current->mm->locked_vm -= old_size;
  562. if (size == 0)
  563. goto out;
  564. rlim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
  565. vm = current->mm->total_vm + size;
  566. if (rlim < vm) {
  567. ret = -ENOMEM;
  568. if (!reduce_size)
  569. goto out;
  570. size = rlim - current->mm->total_vm;
  571. if (size <= 0)
  572. goto out;
  573. }
  574. rlim = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
  575. vm = current->mm->locked_vm + size;
  576. if (rlim < vm) {
  577. ret = -ENOMEM;
  578. if (!reduce_size)
  579. goto out;
  580. size = rlim - current->mm->locked_vm;
  581. if (size <= 0)
  582. goto out;
  583. }
  584. ret = ds_allocate((void **)&child->thread.ds_area_msr,
  585. size << PAGE_SHIFT);
  586. if (ret < 0)
  587. goto out;
  588. current->mm->total_vm += size;
  589. current->mm->locked_vm += size;
  590. out:
  591. if (child->thread.ds_area_msr)
  592. set_tsk_thread_flag(child, TIF_DS_AREA_MSR);
  593. else
  594. clear_tsk_thread_flag(child, TIF_DS_AREA_MSR);
  595. return ret;
  596. }
  597. static int ptrace_bts_config(struct task_struct *child,
  598. const struct ptrace_bts_config __user *ucfg)
  599. {
  600. struct ptrace_bts_config cfg;
  601. int bts_size, ret = 0;
  602. void *ds;
  603. if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
  604. return -EFAULT;
  605. bts_size = 0;
  606. ds = (void *)child->thread.ds_area_msr;
  607. if (ds) {
  608. bts_size = ds_get_bts_size(ds);
  609. if (bts_size < 0)
  610. return bts_size;
  611. }
  612. cfg.size = PAGE_ALIGN(cfg.size);
  613. if (bts_size != cfg.size) {
  614. ret = ptrace_bts_realloc(child, cfg.size,
  615. cfg.flags & PTRACE_BTS_O_CUT_SIZE);
  616. if (ret < 0)
  617. goto errout;
  618. ds = (void *)child->thread.ds_area_msr;
  619. }
  620. if (cfg.flags & PTRACE_BTS_O_SIGNAL)
  621. ret = ds_set_overflow(ds, DS_O_SIGNAL);
  622. else
  623. ret = ds_set_overflow(ds, DS_O_WRAP);
  624. if (ret < 0)
  625. goto errout;
  626. if (cfg.flags & PTRACE_BTS_O_TRACE)
  627. child->thread.debugctlmsr |= ds_debugctl_mask();
  628. else
  629. child->thread.debugctlmsr &= ~ds_debugctl_mask();
  630. if (cfg.flags & PTRACE_BTS_O_SCHED)
  631. set_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
  632. else
  633. clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
  634. out:
  635. if (child->thread.debugctlmsr)
  636. set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
  637. else
  638. clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
  639. return ret;
  640. errout:
  641. child->thread.debugctlmsr &= ~ds_debugctl_mask();
  642. clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
  643. goto out;
  644. }
  645. static int ptrace_bts_status(struct task_struct *child,
  646. struct ptrace_bts_config __user *ucfg)
  647. {
  648. void *ds = (void *)child->thread.ds_area_msr;
  649. struct ptrace_bts_config cfg;
  650. memset(&cfg, 0, sizeof(cfg));
  651. if (ds) {
  652. cfg.size = ds_get_bts_size(ds);
  653. if (ds_get_overflow(ds) == DS_O_SIGNAL)
  654. cfg.flags |= PTRACE_BTS_O_SIGNAL;
  655. if (test_tsk_thread_flag(child, TIF_DEBUGCTLMSR) &&
  656. child->thread.debugctlmsr & ds_debugctl_mask())
  657. cfg.flags |= PTRACE_BTS_O_TRACE;
  658. if (test_tsk_thread_flag(child, TIF_BTS_TRACE_TS))
  659. cfg.flags |= PTRACE_BTS_O_SCHED;
  660. }
  661. if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
  662. return -EFAULT;
  663. return sizeof(cfg);
  664. }
  665. void ptrace_bts_take_timestamp(struct task_struct *tsk,
  666. enum bts_qualifier qualifier)
  667. {
  668. struct bts_struct rec = {
  669. .qualifier = qualifier,
  670. .variant.jiffies = jiffies_64
  671. };
  672. ptrace_bts_write_record(tsk, &rec);
  673. }
  674. /*
  675. * Called by kernel/ptrace.c when detaching..
  676. *
  677. * Make sure the single step bit is not set.
  678. */
  679. void ptrace_disable(struct task_struct *child)
  680. {
  681. user_disable_single_step(child);
  682. #ifdef TIF_SYSCALL_EMU
  683. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  684. #endif
  685. if (child->thread.ds_area_msr) {
  686. ptrace_bts_realloc(child, 0, 0);
  687. child->thread.debugctlmsr &= ~ds_debugctl_mask();
  688. if (!child->thread.debugctlmsr)
  689. clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
  690. clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
  691. }
  692. }
  693. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  694. static const struct user_regset_view user_x86_32_view; /* Initialized below. */
  695. #endif
  696. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  697. {
  698. int ret;
  699. unsigned long __user *datap = (unsigned long __user *)data;
  700. switch (request) {
  701. /* read the word at location addr in the USER area. */
  702. case PTRACE_PEEKUSR: {
  703. unsigned long tmp;
  704. ret = -EIO;
  705. if ((addr & (sizeof(data) - 1)) || addr < 0 ||
  706. addr >= sizeof(struct user))
  707. break;
  708. tmp = 0; /* Default return condition */
  709. if (addr < sizeof(struct user_regs_struct))
  710. tmp = getreg(child, addr);
  711. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  712. addr <= offsetof(struct user, u_debugreg[7])) {
  713. addr -= offsetof(struct user, u_debugreg[0]);
  714. tmp = ptrace_get_debugreg(child, addr / sizeof(data));
  715. }
  716. ret = put_user(tmp, datap);
  717. break;
  718. }
  719. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  720. ret = -EIO;
  721. if ((addr & (sizeof(data) - 1)) || addr < 0 ||
  722. addr >= sizeof(struct user))
  723. break;
  724. if (addr < sizeof(struct user_regs_struct))
  725. ret = putreg(child, addr, data);
  726. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  727. addr <= offsetof(struct user, u_debugreg[7])) {
  728. addr -= offsetof(struct user, u_debugreg[0]);
  729. ret = ptrace_set_debugreg(child,
  730. addr / sizeof(data), data);
  731. }
  732. break;
  733. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  734. return copy_regset_to_user(child,
  735. task_user_regset_view(current),
  736. REGSET_GENERAL,
  737. 0, sizeof(struct user_regs_struct),
  738. datap);
  739. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  740. return copy_regset_from_user(child,
  741. task_user_regset_view(current),
  742. REGSET_GENERAL,
  743. 0, sizeof(struct user_regs_struct),
  744. datap);
  745. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  746. return copy_regset_to_user(child,
  747. task_user_regset_view(current),
  748. REGSET_FP,
  749. 0, sizeof(struct user_i387_struct),
  750. datap);
  751. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  752. return copy_regset_from_user(child,
  753. task_user_regset_view(current),
  754. REGSET_FP,
  755. 0, sizeof(struct user_i387_struct),
  756. datap);
  757. #ifdef CONFIG_X86_32
  758. case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
  759. return copy_regset_to_user(child, &user_x86_32_view,
  760. REGSET_XFP,
  761. 0, sizeof(struct user_fxsr_struct),
  762. datap);
  763. case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
  764. return copy_regset_from_user(child, &user_x86_32_view,
  765. REGSET_XFP,
  766. 0, sizeof(struct user_fxsr_struct),
  767. datap);
  768. #endif
  769. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  770. case PTRACE_GET_THREAD_AREA:
  771. if (addr < 0)
  772. return -EIO;
  773. ret = do_get_thread_area(child, addr,
  774. (struct user_desc __user *) data);
  775. break;
  776. case PTRACE_SET_THREAD_AREA:
  777. if (addr < 0)
  778. return -EIO;
  779. ret = do_set_thread_area(child, addr,
  780. (struct user_desc __user *) data, 0);
  781. break;
  782. #endif
  783. #ifdef CONFIG_X86_64
  784. /* normal 64bit interface to access TLS data.
  785. Works just like arch_prctl, except that the arguments
  786. are reversed. */
  787. case PTRACE_ARCH_PRCTL:
  788. ret = do_arch_prctl(child, data, addr);
  789. break;
  790. #endif
  791. case PTRACE_BTS_CONFIG:
  792. ret = ptrace_bts_config
  793. (child, (struct ptrace_bts_config __user *)addr);
  794. break;
  795. case PTRACE_BTS_STATUS:
  796. ret = ptrace_bts_status
  797. (child, (struct ptrace_bts_config __user *)addr);
  798. break;
  799. case PTRACE_BTS_SIZE:
  800. ret = ptrace_bts_get_size(child);
  801. break;
  802. case PTRACE_BTS_GET:
  803. ret = ptrace_bts_read_record
  804. (child, data, (struct bts_struct __user *) addr);
  805. break;
  806. case PTRACE_BTS_CLEAR:
  807. ret = ptrace_bts_clear(child);
  808. break;
  809. case PTRACE_BTS_DRAIN:
  810. ret = ptrace_bts_drain
  811. (child, (struct bts_struct __user *) addr);
  812. break;
  813. default:
  814. ret = ptrace_request(child, request, addr, data);
  815. break;
  816. }
  817. return ret;
  818. }
  819. #ifdef CONFIG_IA32_EMULATION
  820. #include <linux/compat.h>
  821. #include <linux/syscalls.h>
  822. #include <asm/ia32.h>
  823. #include <asm/user32.h>
  824. #define R32(l,q) \
  825. case offsetof(struct user32, regs.l): \
  826. regs->q = value; break
  827. #define SEG32(rs) \
  828. case offsetof(struct user32, regs.rs): \
  829. return set_segment_reg(child, \
  830. offsetof(struct user_regs_struct, rs), \
  831. value); \
  832. break
  833. static int putreg32(struct task_struct *child, unsigned regno, u32 value)
  834. {
  835. struct pt_regs *regs = task_pt_regs(child);
  836. switch (regno) {
  837. SEG32(cs);
  838. SEG32(ds);
  839. SEG32(es);
  840. SEG32(fs);
  841. SEG32(gs);
  842. SEG32(ss);
  843. R32(ebx, bx);
  844. R32(ecx, cx);
  845. R32(edx, dx);
  846. R32(edi, di);
  847. R32(esi, si);
  848. R32(ebp, bp);
  849. R32(eax, ax);
  850. R32(orig_eax, orig_ax);
  851. R32(eip, ip);
  852. R32(esp, sp);
  853. case offsetof(struct user32, regs.eflags):
  854. return set_flags(child, value);
  855. case offsetof(struct user32, u_debugreg[0]) ...
  856. offsetof(struct user32, u_debugreg[7]):
  857. regno -= offsetof(struct user32, u_debugreg[0]);
  858. return ptrace_set_debugreg(child, regno / 4, value);
  859. default:
  860. if (regno > sizeof(struct user32) || (regno & 3))
  861. return -EIO;
  862. /*
  863. * Other dummy fields in the virtual user structure
  864. * are ignored
  865. */
  866. break;
  867. }
  868. return 0;
  869. }
  870. #undef R32
  871. #undef SEG32
  872. #define R32(l,q) \
  873. case offsetof(struct user32, regs.l): \
  874. *val = regs->q; break
  875. #define SEG32(rs) \
  876. case offsetof(struct user32, regs.rs): \
  877. *val = get_segment_reg(child, \
  878. offsetof(struct user_regs_struct, rs)); \
  879. break
  880. static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
  881. {
  882. struct pt_regs *regs = task_pt_regs(child);
  883. switch (regno) {
  884. SEG32(ds);
  885. SEG32(es);
  886. SEG32(fs);
  887. SEG32(gs);
  888. R32(cs, cs);
  889. R32(ss, ss);
  890. R32(ebx, bx);
  891. R32(ecx, cx);
  892. R32(edx, dx);
  893. R32(edi, di);
  894. R32(esi, si);
  895. R32(ebp, bp);
  896. R32(eax, ax);
  897. R32(orig_eax, orig_ax);
  898. R32(eip, ip);
  899. R32(esp, sp);
  900. case offsetof(struct user32, regs.eflags):
  901. *val = get_flags(child);
  902. break;
  903. case offsetof(struct user32, u_debugreg[0]) ...
  904. offsetof(struct user32, u_debugreg[7]):
  905. regno -= offsetof(struct user32, u_debugreg[0]);
  906. *val = ptrace_get_debugreg(child, regno / 4);
  907. break;
  908. default:
  909. if (regno > sizeof(struct user32) || (regno & 3))
  910. return -EIO;
  911. /*
  912. * Other dummy fields in the virtual user structure
  913. * are ignored
  914. */
  915. *val = 0;
  916. break;
  917. }
  918. return 0;
  919. }
  920. #undef R32
  921. #undef SEG32
  922. static int genregs32_get(struct task_struct *target,
  923. const struct user_regset *regset,
  924. unsigned int pos, unsigned int count,
  925. void *kbuf, void __user *ubuf)
  926. {
  927. if (kbuf) {
  928. compat_ulong_t *k = kbuf;
  929. while (count > 0) {
  930. getreg32(target, pos, k++);
  931. count -= sizeof(*k);
  932. pos += sizeof(*k);
  933. }
  934. } else {
  935. compat_ulong_t __user *u = ubuf;
  936. while (count > 0) {
  937. compat_ulong_t word;
  938. getreg32(target, pos, &word);
  939. if (__put_user(word, u++))
  940. return -EFAULT;
  941. count -= sizeof(*u);
  942. pos += sizeof(*u);
  943. }
  944. }
  945. return 0;
  946. }
  947. static int genregs32_set(struct task_struct *target,
  948. const struct user_regset *regset,
  949. unsigned int pos, unsigned int count,
  950. const void *kbuf, const void __user *ubuf)
  951. {
  952. int ret = 0;
  953. if (kbuf) {
  954. const compat_ulong_t *k = kbuf;
  955. while (count > 0 && !ret) {
  956. ret = putreg(target, pos, *k++);
  957. count -= sizeof(*k);
  958. pos += sizeof(*k);
  959. }
  960. } else {
  961. const compat_ulong_t __user *u = ubuf;
  962. while (count > 0 && !ret) {
  963. compat_ulong_t word;
  964. ret = __get_user(word, u++);
  965. if (ret)
  966. break;
  967. ret = putreg(target, pos, word);
  968. count -= sizeof(*u);
  969. pos += sizeof(*u);
  970. }
  971. }
  972. return ret;
  973. }
  974. static long ptrace32_siginfo(unsigned request, u32 pid, u32 addr, u32 data)
  975. {
  976. siginfo_t __user *si = compat_alloc_user_space(sizeof(siginfo_t));
  977. compat_siginfo_t __user *si32 = compat_ptr(data);
  978. siginfo_t ssi;
  979. int ret;
  980. if (request == PTRACE_SETSIGINFO) {
  981. memset(&ssi, 0, sizeof(siginfo_t));
  982. ret = copy_siginfo_from_user32(&ssi, si32);
  983. if (ret)
  984. return ret;
  985. if (copy_to_user(si, &ssi, sizeof(siginfo_t)))
  986. return -EFAULT;
  987. }
  988. ret = sys_ptrace(request, pid, addr, (unsigned long)si);
  989. if (ret)
  990. return ret;
  991. if (request == PTRACE_GETSIGINFO) {
  992. if (copy_from_user(&ssi, si, sizeof(siginfo_t)))
  993. return -EFAULT;
  994. ret = copy_siginfo_to_user32(si32, &ssi);
  995. }
  996. return ret;
  997. }
  998. asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
  999. {
  1000. struct task_struct *child;
  1001. struct pt_regs *childregs;
  1002. void __user *datap = compat_ptr(data);
  1003. int ret;
  1004. __u32 val;
  1005. switch (request) {
  1006. case PTRACE_TRACEME:
  1007. case PTRACE_ATTACH:
  1008. case PTRACE_KILL:
  1009. case PTRACE_CONT:
  1010. case PTRACE_SINGLESTEP:
  1011. case PTRACE_SINGLEBLOCK:
  1012. case PTRACE_DETACH:
  1013. case PTRACE_SYSCALL:
  1014. case PTRACE_OLDSETOPTIONS:
  1015. case PTRACE_SETOPTIONS:
  1016. case PTRACE_SET_THREAD_AREA:
  1017. case PTRACE_GET_THREAD_AREA:
  1018. case PTRACE_BTS_CONFIG:
  1019. case PTRACE_BTS_STATUS:
  1020. case PTRACE_BTS_SIZE:
  1021. case PTRACE_BTS_GET:
  1022. case PTRACE_BTS_CLEAR:
  1023. case PTRACE_BTS_DRAIN:
  1024. return sys_ptrace(request, pid, addr, data);
  1025. default:
  1026. return -EINVAL;
  1027. case PTRACE_PEEKTEXT:
  1028. case PTRACE_PEEKDATA:
  1029. case PTRACE_POKEDATA:
  1030. case PTRACE_POKETEXT:
  1031. case PTRACE_POKEUSR:
  1032. case PTRACE_PEEKUSR:
  1033. case PTRACE_GETREGS:
  1034. case PTRACE_SETREGS:
  1035. case PTRACE_SETFPREGS:
  1036. case PTRACE_GETFPREGS:
  1037. case PTRACE_SETFPXREGS:
  1038. case PTRACE_GETFPXREGS:
  1039. case PTRACE_GETEVENTMSG:
  1040. break;
  1041. case PTRACE_SETSIGINFO:
  1042. case PTRACE_GETSIGINFO:
  1043. return ptrace32_siginfo(request, pid, addr, data);
  1044. }
  1045. child = ptrace_get_task_struct(pid);
  1046. if (IS_ERR(child))
  1047. return PTR_ERR(child);
  1048. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  1049. if (ret < 0)
  1050. goto out;
  1051. childregs = task_pt_regs(child);
  1052. switch (request) {
  1053. case PTRACE_PEEKUSR:
  1054. ret = getreg32(child, addr, &val);
  1055. if (ret == 0)
  1056. ret = put_user(val, (__u32 __user *)datap);
  1057. break;
  1058. case PTRACE_POKEUSR:
  1059. ret = putreg32(child, addr, data);
  1060. break;
  1061. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  1062. return copy_regset_to_user(child, &user_x86_32_view,
  1063. REGSET_GENERAL,
  1064. 0, sizeof(struct user_regs_struct32),
  1065. datap);
  1066. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  1067. return copy_regset_from_user(child, &user_x86_32_view,
  1068. REGSET_GENERAL, 0,
  1069. sizeof(struct user_regs_struct32),
  1070. datap);
  1071. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  1072. return copy_regset_to_user(child, &user_x86_32_view,
  1073. REGSET_FP, 0,
  1074. sizeof(struct user_i387_ia32_struct),
  1075. datap);
  1076. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  1077. return copy_regset_from_user(
  1078. child, &user_x86_32_view, REGSET_FP,
  1079. 0, sizeof(struct user_i387_ia32_struct), datap);
  1080. case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
  1081. return copy_regset_to_user(child, &user_x86_32_view,
  1082. REGSET_XFP, 0,
  1083. sizeof(struct user32_fxsr_struct),
  1084. datap);
  1085. case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
  1086. return copy_regset_from_user(child, &user_x86_32_view,
  1087. REGSET_XFP, 0,
  1088. sizeof(struct user32_fxsr_struct),
  1089. datap);
  1090. default:
  1091. return compat_ptrace_request(child, request, addr, data);
  1092. }
  1093. out:
  1094. put_task_struct(child);
  1095. return ret;
  1096. }
  1097. #endif /* CONFIG_IA32_EMULATION */
  1098. #ifdef CONFIG_X86_64
  1099. static const struct user_regset x86_64_regsets[] = {
  1100. [REGSET_GENERAL] = {
  1101. .core_note_type = NT_PRSTATUS,
  1102. .n = sizeof(struct user_regs_struct) / sizeof(long),
  1103. .size = sizeof(long), .align = sizeof(long),
  1104. .get = genregs_get, .set = genregs_set
  1105. },
  1106. [REGSET_FP] = {
  1107. .core_note_type = NT_PRFPREG,
  1108. .n = sizeof(struct user_i387_struct) / sizeof(long),
  1109. .size = sizeof(long), .align = sizeof(long),
  1110. .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
  1111. },
  1112. };
  1113. static const struct user_regset_view user_x86_64_view = {
  1114. .name = "x86_64", .e_machine = EM_X86_64,
  1115. .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
  1116. };
  1117. #else /* CONFIG_X86_32 */
  1118. #define user_regs_struct32 user_regs_struct
  1119. #define genregs32_get genregs_get
  1120. #define genregs32_set genregs_set
  1121. #endif /* CONFIG_X86_64 */
  1122. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1123. static const struct user_regset x86_32_regsets[] = {
  1124. [REGSET_GENERAL] = {
  1125. .core_note_type = NT_PRSTATUS,
  1126. .n = sizeof(struct user_regs_struct32) / sizeof(u32),
  1127. .size = sizeof(u32), .align = sizeof(u32),
  1128. .get = genregs32_get, .set = genregs32_set
  1129. },
  1130. [REGSET_FP] = {
  1131. .core_note_type = NT_PRFPREG,
  1132. .n = sizeof(struct user_i387_struct) / sizeof(u32),
  1133. .size = sizeof(u32), .align = sizeof(u32),
  1134. .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
  1135. },
  1136. [REGSET_XFP] = {
  1137. .core_note_type = NT_PRXFPREG,
  1138. .n = sizeof(struct user_i387_struct) / sizeof(u32),
  1139. .size = sizeof(u32), .align = sizeof(u32),
  1140. .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
  1141. },
  1142. [REGSET_TLS] = {
  1143. .core_note_type = NT_386_TLS,
  1144. .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
  1145. .size = sizeof(struct user_desc),
  1146. .align = sizeof(struct user_desc),
  1147. .active = regset_tls_active,
  1148. .get = regset_tls_get, .set = regset_tls_set
  1149. },
  1150. };
  1151. static const struct user_regset_view user_x86_32_view = {
  1152. .name = "i386", .e_machine = EM_386,
  1153. .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
  1154. };
  1155. #endif
  1156. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  1157. {
  1158. #ifdef CONFIG_IA32_EMULATION
  1159. if (test_tsk_thread_flag(task, TIF_IA32))
  1160. #endif
  1161. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  1162. return &user_x86_32_view;
  1163. #endif
  1164. #ifdef CONFIG_X86_64
  1165. return &user_x86_64_view;
  1166. #endif
  1167. }
  1168. #ifdef CONFIG_X86_32
  1169. void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)
  1170. {
  1171. struct siginfo info;
  1172. tsk->thread.trap_no = 1;
  1173. tsk->thread.error_code = error_code;
  1174. memset(&info, 0, sizeof(info));
  1175. info.si_signo = SIGTRAP;
  1176. info.si_code = TRAP_BRKPT;
  1177. /* User-mode ip? */
  1178. info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
  1179. /* Send us the fake SIGTRAP */
  1180. force_sig_info(SIGTRAP, &info, tsk);
  1181. }
  1182. /* notification of system call entry/exit
  1183. * - triggered by current->work.syscall_trace
  1184. */
  1185. __attribute__((regparm(3)))
  1186. int do_syscall_trace(struct pt_regs *regs, int entryexit)
  1187. {
  1188. int is_sysemu = test_thread_flag(TIF_SYSCALL_EMU);
  1189. /*
  1190. * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall
  1191. * interception
  1192. */
  1193. int is_singlestep = !is_sysemu && test_thread_flag(TIF_SINGLESTEP);
  1194. int ret = 0;
  1195. /* do the secure computing check first */
  1196. if (!entryexit)
  1197. secure_computing(regs->orig_ax);
  1198. if (unlikely(current->audit_context)) {
  1199. if (entryexit)
  1200. audit_syscall_exit(AUDITSC_RESULT(regs->ax),
  1201. regs->ax);
  1202. /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only
  1203. * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is
  1204. * not used, entry.S will call us only on syscall exit, not
  1205. * entry; so when TIF_SYSCALL_AUDIT is used we must avoid
  1206. * calling send_sigtrap() on syscall entry.
  1207. *
  1208. * Note that when PTRACE_SYSEMU_SINGLESTEP is used,
  1209. * is_singlestep is false, despite his name, so we will still do
  1210. * the correct thing.
  1211. */
  1212. else if (is_singlestep)
  1213. goto out;
  1214. }
  1215. if (!(current->ptrace & PT_PTRACED))
  1216. goto out;
  1217. /* If a process stops on the 1st tracepoint with SYSCALL_TRACE
  1218. * and then is resumed with SYSEMU_SINGLESTEP, it will come in
  1219. * here. We have to check this and return */
  1220. if (is_sysemu && entryexit)
  1221. return 0;
  1222. /* Fake a debug trap */
  1223. if (is_singlestep)
  1224. send_sigtrap(current, regs, 0);
  1225. if (!test_thread_flag(TIF_SYSCALL_TRACE) && !is_sysemu)
  1226. goto out;
  1227. /* the 0x80 provides a way for the tracing parent to distinguish
  1228. between a syscall stop and SIGTRAP delivery */
  1229. /* Note that the debugger could change the result of test_thread_flag!*/
  1230. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80:0));
  1231. /*
  1232. * this isn't the same as continuing with a signal, but it will do
  1233. * for normal use. strace only continues with a signal if the
  1234. * stopping signal is not SIGTRAP. -brl
  1235. */
  1236. if (current->exit_code) {
  1237. send_sig(current->exit_code, current, 1);
  1238. current->exit_code = 0;
  1239. }
  1240. ret = is_sysemu;
  1241. out:
  1242. if (unlikely(current->audit_context) && !entryexit)
  1243. audit_syscall_entry(AUDIT_ARCH_I386, regs->orig_ax,
  1244. regs->bx, regs->cx, regs->dx, regs->si);
  1245. if (ret == 0)
  1246. return 0;
  1247. regs->orig_ax = -1; /* force skip of syscall restarting */
  1248. if (unlikely(current->audit_context))
  1249. audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
  1250. return 1;
  1251. }
  1252. #else /* CONFIG_X86_64 */
  1253. static void syscall_trace(struct pt_regs *regs)
  1254. {
  1255. #if 0
  1256. printk("trace %s ip %lx sp %lx ax %d origrax %d caller %lx tiflags %x ptrace %x\n",
  1257. current->comm,
  1258. regs->ip, regs->sp, regs->ax, regs->orig_ax, __builtin_return_address(0),
  1259. current_thread_info()->flags, current->ptrace);
  1260. #endif
  1261. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  1262. ? 0x80 : 0));
  1263. /*
  1264. * this isn't the same as continuing with a signal, but it will do
  1265. * for normal use. strace only continues with a signal if the
  1266. * stopping signal is not SIGTRAP. -brl
  1267. */
  1268. if (current->exit_code) {
  1269. send_sig(current->exit_code, current, 1);
  1270. current->exit_code = 0;
  1271. }
  1272. }
  1273. asmlinkage void syscall_trace_enter(struct pt_regs *regs)
  1274. {
  1275. /* do the secure computing check first */
  1276. secure_computing(regs->orig_ax);
  1277. if (test_thread_flag(TIF_SYSCALL_TRACE)
  1278. && (current->ptrace & PT_PTRACED))
  1279. syscall_trace(regs);
  1280. if (unlikely(current->audit_context)) {
  1281. if (test_thread_flag(TIF_IA32)) {
  1282. audit_syscall_entry(AUDIT_ARCH_I386,
  1283. regs->orig_ax,
  1284. regs->bx, regs->cx,
  1285. regs->dx, regs->si);
  1286. } else {
  1287. audit_syscall_entry(AUDIT_ARCH_X86_64,
  1288. regs->orig_ax,
  1289. regs->di, regs->si,
  1290. regs->dx, regs->r10);
  1291. }
  1292. }
  1293. }
  1294. asmlinkage void syscall_trace_leave(struct pt_regs *regs)
  1295. {
  1296. if (unlikely(current->audit_context))
  1297. audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
  1298. if ((test_thread_flag(TIF_SYSCALL_TRACE)
  1299. || test_thread_flag(TIF_SINGLESTEP))
  1300. && (current->ptrace & PT_PTRACED))
  1301. syscall_trace(regs);
  1302. }
  1303. #endif /* CONFIG_X86_32 */