ptrace.c 39 KB

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