process_64.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. * Copyright (C) 1995 Linus Torvalds
  3. *
  4. * Pentium III FXSR, SSE support
  5. * Gareth Hughes <gareth@valinux.com>, May 2000
  6. *
  7. * X86-64 port
  8. * Andi Kleen.
  9. *
  10. * CPU hotplug support - ashok.raj@intel.com
  11. */
  12. /*
  13. * This file handles the architecture-dependent parts of process handling..
  14. */
  15. #include <linux/cpu.h>
  16. #include <linux/errno.h>
  17. #include <linux/sched.h>
  18. #include <linux/fs.h>
  19. #include <linux/kernel.h>
  20. #include <linux/mm.h>
  21. #include <linux/elfcore.h>
  22. #include <linux/smp.h>
  23. #include <linux/slab.h>
  24. #include <linux/user.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/delay.h>
  27. #include <linux/module.h>
  28. #include <linux/ptrace.h>
  29. #include <linux/notifier.h>
  30. #include <linux/kprobes.h>
  31. #include <linux/kdebug.h>
  32. #include <linux/prctl.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/io.h>
  35. #include <linux/ftrace.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/processor.h>
  38. #include <asm/i387.h>
  39. #include <asm/fpu-internal.h>
  40. #include <asm/mmu_context.h>
  41. #include <asm/prctl.h>
  42. #include <asm/desc.h>
  43. #include <asm/proto.h>
  44. #include <asm/ia32.h>
  45. #include <asm/idle.h>
  46. #include <asm/syscalls.h>
  47. #include <asm/debugreg.h>
  48. #include <asm/switch_to.h>
  49. asmlinkage extern void ret_from_fork(void);
  50. DEFINE_PER_CPU(unsigned long, old_rsp);
  51. /* Prints also some state that isn't saved in the pt_regs */
  52. void __show_regs(struct pt_regs *regs, int all)
  53. {
  54. unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L, fs, gs, shadowgs;
  55. unsigned long d0, d1, d2, d3, d6, d7;
  56. unsigned int fsindex, gsindex;
  57. unsigned int ds, cs, es;
  58. show_regs_common();
  59. printk(KERN_DEFAULT "RIP: %04lx:[<%016lx>] ", regs->cs & 0xffff, regs->ip);
  60. printk_address(regs->ip, 1);
  61. printk(KERN_DEFAULT "RSP: %04lx:%016lx EFLAGS: %08lx\n", regs->ss,
  62. regs->sp, regs->flags);
  63. printk(KERN_DEFAULT "RAX: %016lx RBX: %016lx RCX: %016lx\n",
  64. regs->ax, regs->bx, regs->cx);
  65. printk(KERN_DEFAULT "RDX: %016lx RSI: %016lx RDI: %016lx\n",
  66. regs->dx, regs->si, regs->di);
  67. printk(KERN_DEFAULT "RBP: %016lx R08: %016lx R09: %016lx\n",
  68. regs->bp, regs->r8, regs->r9);
  69. printk(KERN_DEFAULT "R10: %016lx R11: %016lx R12: %016lx\n",
  70. regs->r10, regs->r11, regs->r12);
  71. printk(KERN_DEFAULT "R13: %016lx R14: %016lx R15: %016lx\n",
  72. regs->r13, regs->r14, regs->r15);
  73. asm("movl %%ds,%0" : "=r" (ds));
  74. asm("movl %%cs,%0" : "=r" (cs));
  75. asm("movl %%es,%0" : "=r" (es));
  76. asm("movl %%fs,%0" : "=r" (fsindex));
  77. asm("movl %%gs,%0" : "=r" (gsindex));
  78. rdmsrl(MSR_FS_BASE, fs);
  79. rdmsrl(MSR_GS_BASE, gs);
  80. rdmsrl(MSR_KERNEL_GS_BASE, shadowgs);
  81. if (!all)
  82. return;
  83. cr0 = read_cr0();
  84. cr2 = read_cr2();
  85. cr3 = read_cr3();
  86. cr4 = read_cr4();
  87. printk(KERN_DEFAULT "FS: %016lx(%04x) GS:%016lx(%04x) knlGS:%016lx\n",
  88. fs, fsindex, gs, gsindex, shadowgs);
  89. printk(KERN_DEFAULT "CS: %04x DS: %04x ES: %04x CR0: %016lx\n", cs, ds,
  90. es, cr0);
  91. printk(KERN_DEFAULT "CR2: %016lx CR3: %016lx CR4: %016lx\n", cr2, cr3,
  92. cr4);
  93. get_debugreg(d0, 0);
  94. get_debugreg(d1, 1);
  95. get_debugreg(d2, 2);
  96. printk(KERN_DEFAULT "DR0: %016lx DR1: %016lx DR2: %016lx\n", d0, d1, d2);
  97. get_debugreg(d3, 3);
  98. get_debugreg(d6, 6);
  99. get_debugreg(d7, 7);
  100. printk(KERN_DEFAULT "DR3: %016lx DR6: %016lx DR7: %016lx\n", d3, d6, d7);
  101. }
  102. void release_thread(struct task_struct *dead_task)
  103. {
  104. if (dead_task->mm) {
  105. if (dead_task->mm->context.size) {
  106. pr_warn("WARNING: dead process %8s still has LDT? <%p/%d>\n",
  107. dead_task->comm,
  108. dead_task->mm->context.ldt,
  109. dead_task->mm->context.size);
  110. BUG();
  111. }
  112. }
  113. }
  114. static inline void set_32bit_tls(struct task_struct *t, int tls, u32 addr)
  115. {
  116. struct user_desc ud = {
  117. .base_addr = addr,
  118. .limit = 0xfffff,
  119. .seg_32bit = 1,
  120. .limit_in_pages = 1,
  121. .useable = 1,
  122. };
  123. struct desc_struct *desc = t->thread.tls_array;
  124. desc += tls;
  125. fill_ldt(desc, &ud);
  126. }
  127. static inline u32 read_32bit_tls(struct task_struct *t, int tls)
  128. {
  129. return get_desc_base(&t->thread.tls_array[tls]);
  130. }
  131. int copy_thread(unsigned long clone_flags, unsigned long sp,
  132. unsigned long unused,
  133. struct task_struct *p, struct pt_regs *regs)
  134. {
  135. int err;
  136. struct pt_regs *childregs;
  137. struct task_struct *me = current;
  138. childregs = ((struct pt_regs *)
  139. (THREAD_SIZE + task_stack_page(p))) - 1;
  140. *childregs = *regs;
  141. childregs->ax = 0;
  142. if (user_mode(regs))
  143. childregs->sp = sp;
  144. else
  145. childregs->sp = (unsigned long)childregs;
  146. p->thread.sp = (unsigned long) childregs;
  147. p->thread.sp0 = (unsigned long) (childregs+1);
  148. p->thread.usersp = me->thread.usersp;
  149. set_tsk_thread_flag(p, TIF_FORK);
  150. p->fpu_counter = 0;
  151. p->thread.io_bitmap_ptr = NULL;
  152. savesegment(gs, p->thread.gsindex);
  153. p->thread.gs = p->thread.gsindex ? 0 : me->thread.gs;
  154. savesegment(fs, p->thread.fsindex);
  155. p->thread.fs = p->thread.fsindex ? 0 : me->thread.fs;
  156. savesegment(es, p->thread.es);
  157. savesegment(ds, p->thread.ds);
  158. err = -ENOMEM;
  159. memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
  160. if (unlikely(test_tsk_thread_flag(me, TIF_IO_BITMAP))) {
  161. p->thread.io_bitmap_ptr = kmemdup(me->thread.io_bitmap_ptr,
  162. IO_BITMAP_BYTES, GFP_KERNEL);
  163. if (!p->thread.io_bitmap_ptr) {
  164. p->thread.io_bitmap_max = 0;
  165. return -ENOMEM;
  166. }
  167. set_tsk_thread_flag(p, TIF_IO_BITMAP);
  168. }
  169. /*
  170. * Set a new TLS for the child thread?
  171. */
  172. if (clone_flags & CLONE_SETTLS) {
  173. #ifdef CONFIG_IA32_EMULATION
  174. if (test_thread_flag(TIF_IA32))
  175. err = do_set_thread_area(p, -1,
  176. (struct user_desc __user *)childregs->si, 0);
  177. else
  178. #endif
  179. err = do_arch_prctl(p, ARCH_SET_FS, childregs->r8);
  180. if (err)
  181. goto out;
  182. }
  183. err = 0;
  184. out:
  185. if (err && p->thread.io_bitmap_ptr) {
  186. kfree(p->thread.io_bitmap_ptr);
  187. p->thread.io_bitmap_max = 0;
  188. }
  189. return err;
  190. }
  191. static void
  192. start_thread_common(struct pt_regs *regs, unsigned long new_ip,
  193. unsigned long new_sp,
  194. unsigned int _cs, unsigned int _ss, unsigned int _ds)
  195. {
  196. loadsegment(fs, 0);
  197. loadsegment(es, _ds);
  198. loadsegment(ds, _ds);
  199. load_gs_index(0);
  200. current->thread.usersp = new_sp;
  201. regs->ip = new_ip;
  202. regs->sp = new_sp;
  203. this_cpu_write(old_rsp, new_sp);
  204. regs->cs = _cs;
  205. regs->ss = _ss;
  206. regs->flags = X86_EFLAGS_IF;
  207. }
  208. void
  209. start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
  210. {
  211. start_thread_common(regs, new_ip, new_sp,
  212. __USER_CS, __USER_DS, 0);
  213. }
  214. #ifdef CONFIG_IA32_EMULATION
  215. void start_thread_ia32(struct pt_regs *regs, u32 new_ip, u32 new_sp)
  216. {
  217. start_thread_common(regs, new_ip, new_sp,
  218. test_thread_flag(TIF_X32)
  219. ? __USER_CS : __USER32_CS,
  220. __USER_DS, __USER_DS);
  221. }
  222. #endif
  223. /*
  224. * switch_to(x,y) should switch tasks from x to y.
  225. *
  226. * This could still be optimized:
  227. * - fold all the options into a flag word and test it with a single test.
  228. * - could test fs/gs bitsliced
  229. *
  230. * Kprobes not supported here. Set the probe on schedule instead.
  231. * Function graph tracer not supported too.
  232. */
  233. __notrace_funcgraph struct task_struct *
  234. __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
  235. {
  236. struct thread_struct *prev = &prev_p->thread;
  237. struct thread_struct *next = &next_p->thread;
  238. int cpu = smp_processor_id();
  239. struct tss_struct *tss = &per_cpu(init_tss, cpu);
  240. unsigned fsindex, gsindex;
  241. fpu_switch_t fpu;
  242. fpu = switch_fpu_prepare(prev_p, next_p, cpu);
  243. /*
  244. * Reload esp0, LDT and the page table pointer:
  245. */
  246. load_sp0(tss, next);
  247. /*
  248. * Switch DS and ES.
  249. * This won't pick up thread selector changes, but I guess that is ok.
  250. */
  251. savesegment(es, prev->es);
  252. if (unlikely(next->es | prev->es))
  253. loadsegment(es, next->es);
  254. savesegment(ds, prev->ds);
  255. if (unlikely(next->ds | prev->ds))
  256. loadsegment(ds, next->ds);
  257. /* We must save %fs and %gs before load_TLS() because
  258. * %fs and %gs may be cleared by load_TLS().
  259. *
  260. * (e.g. xen_load_tls())
  261. */
  262. savesegment(fs, fsindex);
  263. savesegment(gs, gsindex);
  264. load_TLS(next, cpu);
  265. /*
  266. * Leave lazy mode, flushing any hypercalls made here.
  267. * This must be done before restoring TLS segments so
  268. * the GDT and LDT are properly updated, and must be
  269. * done before math_state_restore, so the TS bit is up
  270. * to date.
  271. */
  272. arch_end_context_switch(next_p);
  273. /*
  274. * Switch FS and GS.
  275. *
  276. * Segment register != 0 always requires a reload. Also
  277. * reload when it has changed. When prev process used 64bit
  278. * base always reload to avoid an information leak.
  279. */
  280. if (unlikely(fsindex | next->fsindex | prev->fs)) {
  281. loadsegment(fs, next->fsindex);
  282. /*
  283. * Check if the user used a selector != 0; if yes
  284. * clear 64bit base, since overloaded base is always
  285. * mapped to the Null selector
  286. */
  287. if (fsindex)
  288. prev->fs = 0;
  289. }
  290. /* when next process has a 64bit base use it */
  291. if (next->fs)
  292. wrmsrl(MSR_FS_BASE, next->fs);
  293. prev->fsindex = fsindex;
  294. if (unlikely(gsindex | next->gsindex | prev->gs)) {
  295. load_gs_index(next->gsindex);
  296. if (gsindex)
  297. prev->gs = 0;
  298. }
  299. if (next->gs)
  300. wrmsrl(MSR_KERNEL_GS_BASE, next->gs);
  301. prev->gsindex = gsindex;
  302. switch_fpu_finish(next_p, fpu);
  303. /*
  304. * Switch the PDA and FPU contexts.
  305. */
  306. prev->usersp = this_cpu_read(old_rsp);
  307. this_cpu_write(old_rsp, next->usersp);
  308. this_cpu_write(current_task, next_p);
  309. this_cpu_write(kernel_stack,
  310. (unsigned long)task_stack_page(next_p) +
  311. THREAD_SIZE - KERNEL_STACK_OFFSET);
  312. /*
  313. * Now maybe reload the debug registers and handle I/O bitmaps
  314. */
  315. if (unlikely(task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT ||
  316. task_thread_info(prev_p)->flags & _TIF_WORK_CTXSW_PREV))
  317. __switch_to_xtra(prev_p, next_p, tss);
  318. return prev_p;
  319. }
  320. void set_personality_64bit(void)
  321. {
  322. /* inherit personality from parent */
  323. /* Make sure to be in 64bit mode */
  324. clear_thread_flag(TIF_IA32);
  325. clear_thread_flag(TIF_ADDR32);
  326. clear_thread_flag(TIF_X32);
  327. /* Ensure the corresponding mm is not marked. */
  328. if (current->mm)
  329. current->mm->context.ia32_compat = 0;
  330. /* TBD: overwrites user setup. Should have two bits.
  331. But 64bit processes have always behaved this way,
  332. so it's not too bad. The main problem is just that
  333. 32bit childs are affected again. */
  334. current->personality &= ~READ_IMPLIES_EXEC;
  335. }
  336. void set_personality_ia32(bool x32)
  337. {
  338. /* inherit personality from parent */
  339. /* Make sure to be in 32bit mode */
  340. set_thread_flag(TIF_ADDR32);
  341. /* Mark the associated mm as containing 32-bit tasks. */
  342. if (current->mm)
  343. current->mm->context.ia32_compat = 1;
  344. if (x32) {
  345. clear_thread_flag(TIF_IA32);
  346. set_thread_flag(TIF_X32);
  347. current->personality &= ~READ_IMPLIES_EXEC;
  348. /* is_compat_task() uses the presence of the x32
  349. syscall bit flag to determine compat status */
  350. current_thread_info()->status &= ~TS_COMPAT;
  351. } else {
  352. set_thread_flag(TIF_IA32);
  353. clear_thread_flag(TIF_X32);
  354. current->personality |= force_personality32;
  355. /* Prepare the first "return" to user space */
  356. current_thread_info()->status |= TS_COMPAT;
  357. }
  358. }
  359. EXPORT_SYMBOL_GPL(set_personality_ia32);
  360. unsigned long get_wchan(struct task_struct *p)
  361. {
  362. unsigned long stack;
  363. u64 fp, ip;
  364. int count = 0;
  365. if (!p || p == current || p->state == TASK_RUNNING)
  366. return 0;
  367. stack = (unsigned long)task_stack_page(p);
  368. if (p->thread.sp < stack || p->thread.sp >= stack+THREAD_SIZE)
  369. return 0;
  370. fp = *(u64 *)(p->thread.sp);
  371. do {
  372. if (fp < (unsigned long)stack ||
  373. fp >= (unsigned long)stack+THREAD_SIZE)
  374. return 0;
  375. ip = *(u64 *)(fp+8);
  376. if (!in_sched_functions(ip))
  377. return ip;
  378. fp = *(u64 *)fp;
  379. } while (count++ < 16);
  380. return 0;
  381. }
  382. long do_arch_prctl(struct task_struct *task, int code, unsigned long addr)
  383. {
  384. int ret = 0;
  385. int doit = task == current;
  386. int cpu;
  387. switch (code) {
  388. case ARCH_SET_GS:
  389. if (addr >= TASK_SIZE_OF(task))
  390. return -EPERM;
  391. cpu = get_cpu();
  392. /* handle small bases via the GDT because that's faster to
  393. switch. */
  394. if (addr <= 0xffffffff) {
  395. set_32bit_tls(task, GS_TLS, addr);
  396. if (doit) {
  397. load_TLS(&task->thread, cpu);
  398. load_gs_index(GS_TLS_SEL);
  399. }
  400. task->thread.gsindex = GS_TLS_SEL;
  401. task->thread.gs = 0;
  402. } else {
  403. task->thread.gsindex = 0;
  404. task->thread.gs = addr;
  405. if (doit) {
  406. load_gs_index(0);
  407. ret = wrmsrl_safe(MSR_KERNEL_GS_BASE, addr);
  408. }
  409. }
  410. put_cpu();
  411. break;
  412. case ARCH_SET_FS:
  413. /* Not strictly needed for fs, but do it for symmetry
  414. with gs */
  415. if (addr >= TASK_SIZE_OF(task))
  416. return -EPERM;
  417. cpu = get_cpu();
  418. /* handle small bases via the GDT because that's faster to
  419. switch. */
  420. if (addr <= 0xffffffff) {
  421. set_32bit_tls(task, FS_TLS, addr);
  422. if (doit) {
  423. load_TLS(&task->thread, cpu);
  424. loadsegment(fs, FS_TLS_SEL);
  425. }
  426. task->thread.fsindex = FS_TLS_SEL;
  427. task->thread.fs = 0;
  428. } else {
  429. task->thread.fsindex = 0;
  430. task->thread.fs = addr;
  431. if (doit) {
  432. /* set the selector to 0 to not confuse
  433. __switch_to */
  434. loadsegment(fs, 0);
  435. ret = wrmsrl_safe(MSR_FS_BASE, addr);
  436. }
  437. }
  438. put_cpu();
  439. break;
  440. case ARCH_GET_FS: {
  441. unsigned long base;
  442. if (task->thread.fsindex == FS_TLS_SEL)
  443. base = read_32bit_tls(task, FS_TLS);
  444. else if (doit)
  445. rdmsrl(MSR_FS_BASE, base);
  446. else
  447. base = task->thread.fs;
  448. ret = put_user(base, (unsigned long __user *)addr);
  449. break;
  450. }
  451. case ARCH_GET_GS: {
  452. unsigned long base;
  453. unsigned gsindex;
  454. if (task->thread.gsindex == GS_TLS_SEL)
  455. base = read_32bit_tls(task, GS_TLS);
  456. else if (doit) {
  457. savesegment(gs, gsindex);
  458. if (gsindex)
  459. rdmsrl(MSR_KERNEL_GS_BASE, base);
  460. else
  461. base = task->thread.gs;
  462. } else
  463. base = task->thread.gs;
  464. ret = put_user(base, (unsigned long __user *)addr);
  465. break;
  466. }
  467. default:
  468. ret = -EINVAL;
  469. break;
  470. }
  471. return ret;
  472. }
  473. long sys_arch_prctl(int code, unsigned long addr)
  474. {
  475. return do_arch_prctl(current, code, addr);
  476. }
  477. unsigned long KSTK_ESP(struct task_struct *task)
  478. {
  479. return (test_tsk_thread_flag(task, TIF_IA32)) ?
  480. (task_pt_regs(task)->sp) : ((task)->thread.usersp);
  481. }