ptrace.c 39 KB

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