ptrace.c 38 KB

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