ptrace.c 37 KB

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