process_64.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. printk("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. /*
  132. * This gets called before we allocate a new thread and copy
  133. * the current task into it.
  134. */
  135. void prepare_to_copy(struct task_struct *tsk)
  136. {
  137. unlazy_fpu(tsk);
  138. }
  139. int copy_thread(unsigned long clone_flags, unsigned long sp,
  140. unsigned long unused,
  141. struct task_struct *p, struct pt_regs *regs)
  142. {
  143. int err;
  144. struct pt_regs *childregs;
  145. struct task_struct *me = current;
  146. childregs = ((struct pt_regs *)
  147. (THREAD_SIZE + task_stack_page(p))) - 1;
  148. *childregs = *regs;
  149. childregs->ax = 0;
  150. if (user_mode(regs))
  151. childregs->sp = sp;
  152. else
  153. childregs->sp = (unsigned long)childregs;
  154. p->thread.sp = (unsigned long) childregs;
  155. p->thread.sp0 = (unsigned long) (childregs+1);
  156. p->thread.usersp = me->thread.usersp;
  157. set_tsk_thread_flag(p, TIF_FORK);
  158. p->fpu_counter = 0;
  159. p->thread.io_bitmap_ptr = NULL;
  160. savesegment(gs, p->thread.gsindex);
  161. p->thread.gs = p->thread.gsindex ? 0 : me->thread.gs;
  162. savesegment(fs, p->thread.fsindex);
  163. p->thread.fs = p->thread.fsindex ? 0 : me->thread.fs;
  164. savesegment(es, p->thread.es);
  165. savesegment(ds, p->thread.ds);
  166. err = -ENOMEM;
  167. memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
  168. if (unlikely(test_tsk_thread_flag(me, TIF_IO_BITMAP))) {
  169. p->thread.io_bitmap_ptr = kmemdup(me->thread.io_bitmap_ptr,
  170. IO_BITMAP_BYTES, GFP_KERNEL);
  171. if (!p->thread.io_bitmap_ptr) {
  172. p->thread.io_bitmap_max = 0;
  173. return -ENOMEM;
  174. }
  175. set_tsk_thread_flag(p, TIF_IO_BITMAP);
  176. }
  177. /*
  178. * Set a new TLS for the child thread?
  179. */
  180. if (clone_flags & CLONE_SETTLS) {
  181. #ifdef CONFIG_IA32_EMULATION
  182. if (test_thread_flag(TIF_IA32))
  183. err = do_set_thread_area(p, -1,
  184. (struct user_desc __user *)childregs->si, 0);
  185. else
  186. #endif
  187. err = do_arch_prctl(p, ARCH_SET_FS, childregs->r8);
  188. if (err)
  189. goto out;
  190. }
  191. err = 0;
  192. out:
  193. if (err && p->thread.io_bitmap_ptr) {
  194. kfree(p->thread.io_bitmap_ptr);
  195. p->thread.io_bitmap_max = 0;
  196. }
  197. return err;
  198. }
  199. static void
  200. start_thread_common(struct pt_regs *regs, unsigned long new_ip,
  201. unsigned long new_sp,
  202. unsigned int _cs, unsigned int _ss, unsigned int _ds)
  203. {
  204. loadsegment(fs, 0);
  205. loadsegment(es, _ds);
  206. loadsegment(ds, _ds);
  207. load_gs_index(0);
  208. current->thread.usersp = new_sp;
  209. regs->ip = new_ip;
  210. regs->sp = new_sp;
  211. percpu_write(old_rsp, new_sp);
  212. regs->cs = _cs;
  213. regs->ss = _ss;
  214. regs->flags = X86_EFLAGS_IF;
  215. /*
  216. * Free the old FP and other extended state
  217. */
  218. free_thread_xstate(current);
  219. }
  220. void
  221. start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
  222. {
  223. start_thread_common(regs, new_ip, new_sp,
  224. __USER_CS, __USER_DS, 0);
  225. }
  226. #ifdef CONFIG_IA32_EMULATION
  227. void start_thread_ia32(struct pt_regs *regs, u32 new_ip, u32 new_sp)
  228. {
  229. start_thread_common(regs, new_ip, new_sp,
  230. test_thread_flag(TIF_X32)
  231. ? __USER_CS : __USER32_CS,
  232. __USER_DS, __USER_DS);
  233. }
  234. #endif
  235. /*
  236. * switch_to(x,y) should switch tasks from x to y.
  237. *
  238. * This could still be optimized:
  239. * - fold all the options into a flag word and test it with a single test.
  240. * - could test fs/gs bitsliced
  241. *
  242. * Kprobes not supported here. Set the probe on schedule instead.
  243. * Function graph tracer not supported too.
  244. */
  245. __notrace_funcgraph struct task_struct *
  246. __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
  247. {
  248. struct thread_struct *prev = &prev_p->thread;
  249. struct thread_struct *next = &next_p->thread;
  250. int cpu = smp_processor_id();
  251. struct tss_struct *tss = &per_cpu(init_tss, cpu);
  252. unsigned fsindex, gsindex;
  253. fpu_switch_t fpu;
  254. fpu = switch_fpu_prepare(prev_p, next_p, cpu);
  255. /*
  256. * Reload esp0, LDT and the page table pointer:
  257. */
  258. load_sp0(tss, next);
  259. /*
  260. * Switch DS and ES.
  261. * This won't pick up thread selector changes, but I guess that is ok.
  262. */
  263. savesegment(es, prev->es);
  264. if (unlikely(next->es | prev->es))
  265. loadsegment(es, next->es);
  266. savesegment(ds, prev->ds);
  267. if (unlikely(next->ds | prev->ds))
  268. loadsegment(ds, next->ds);
  269. /* We must save %fs and %gs before load_TLS() because
  270. * %fs and %gs may be cleared by load_TLS().
  271. *
  272. * (e.g. xen_load_tls())
  273. */
  274. savesegment(fs, fsindex);
  275. savesegment(gs, gsindex);
  276. load_TLS(next, cpu);
  277. /*
  278. * Leave lazy mode, flushing any hypercalls made here.
  279. * This must be done before restoring TLS segments so
  280. * the GDT and LDT are properly updated, and must be
  281. * done before math_state_restore, so the TS bit is up
  282. * to date.
  283. */
  284. arch_end_context_switch(next_p);
  285. /*
  286. * Switch FS and GS.
  287. *
  288. * Segment register != 0 always requires a reload. Also
  289. * reload when it has changed. When prev process used 64bit
  290. * base always reload to avoid an information leak.
  291. */
  292. if (unlikely(fsindex | next->fsindex | prev->fs)) {
  293. loadsegment(fs, next->fsindex);
  294. /*
  295. * Check if the user used a selector != 0; if yes
  296. * clear 64bit base, since overloaded base is always
  297. * mapped to the Null selector
  298. */
  299. if (fsindex)
  300. prev->fs = 0;
  301. }
  302. /* when next process has a 64bit base use it */
  303. if (next->fs)
  304. wrmsrl(MSR_FS_BASE, next->fs);
  305. prev->fsindex = fsindex;
  306. if (unlikely(gsindex | next->gsindex | prev->gs)) {
  307. load_gs_index(next->gsindex);
  308. if (gsindex)
  309. prev->gs = 0;
  310. }
  311. if (next->gs)
  312. wrmsrl(MSR_KERNEL_GS_BASE, next->gs);
  313. prev->gsindex = gsindex;
  314. switch_fpu_finish(next_p, fpu);
  315. /*
  316. * Switch the PDA and FPU contexts.
  317. */
  318. prev->usersp = percpu_read(old_rsp);
  319. percpu_write(old_rsp, next->usersp);
  320. percpu_write(current_task, next_p);
  321. percpu_write(kernel_stack,
  322. (unsigned long)task_stack_page(next_p) +
  323. THREAD_SIZE - KERNEL_STACK_OFFSET);
  324. /*
  325. * Now maybe reload the debug registers and handle I/O bitmaps
  326. */
  327. if (unlikely(task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT ||
  328. task_thread_info(prev_p)->flags & _TIF_WORK_CTXSW_PREV))
  329. __switch_to_xtra(prev_p, next_p, tss);
  330. return prev_p;
  331. }
  332. void set_personality_64bit(void)
  333. {
  334. /* inherit personality from parent */
  335. /* Make sure to be in 64bit mode */
  336. clear_thread_flag(TIF_IA32);
  337. clear_thread_flag(TIF_ADDR32);
  338. clear_thread_flag(TIF_X32);
  339. /* Ensure the corresponding mm is not marked. */
  340. if (current->mm)
  341. current->mm->context.ia32_compat = 0;
  342. /* TBD: overwrites user setup. Should have two bits.
  343. But 64bit processes have always behaved this way,
  344. so it's not too bad. The main problem is just that
  345. 32bit childs are affected again. */
  346. current->personality &= ~READ_IMPLIES_EXEC;
  347. }
  348. void set_personality_ia32(bool x32)
  349. {
  350. /* inherit personality from parent */
  351. /* Make sure to be in 32bit mode */
  352. set_thread_flag(TIF_ADDR32);
  353. /* Mark the associated mm as containing 32-bit tasks. */
  354. if (current->mm)
  355. current->mm->context.ia32_compat = 1;
  356. if (x32) {
  357. clear_thread_flag(TIF_IA32);
  358. set_thread_flag(TIF_X32);
  359. current->personality &= ~READ_IMPLIES_EXEC;
  360. /* is_compat_task() uses the presence of the x32
  361. syscall bit flag to determine compat status */
  362. current_thread_info()->status &= ~TS_COMPAT;
  363. } else {
  364. set_thread_flag(TIF_IA32);
  365. clear_thread_flag(TIF_X32);
  366. current->personality |= force_personality32;
  367. /* Prepare the first "return" to user space */
  368. current_thread_info()->status |= TS_COMPAT;
  369. }
  370. }
  371. unsigned long get_wchan(struct task_struct *p)
  372. {
  373. unsigned long stack;
  374. u64 fp, ip;
  375. int count = 0;
  376. if (!p || p == current || p->state == TASK_RUNNING)
  377. return 0;
  378. stack = (unsigned long)task_stack_page(p);
  379. if (p->thread.sp < stack || p->thread.sp >= stack+THREAD_SIZE)
  380. return 0;
  381. fp = *(u64 *)(p->thread.sp);
  382. do {
  383. if (fp < (unsigned long)stack ||
  384. fp >= (unsigned long)stack+THREAD_SIZE)
  385. return 0;
  386. ip = *(u64 *)(fp+8);
  387. if (!in_sched_functions(ip))
  388. return ip;
  389. fp = *(u64 *)fp;
  390. } while (count++ < 16);
  391. return 0;
  392. }
  393. long do_arch_prctl(struct task_struct *task, int code, unsigned long addr)
  394. {
  395. int ret = 0;
  396. int doit = task == current;
  397. int cpu;
  398. switch (code) {
  399. case ARCH_SET_GS:
  400. if (addr >= TASK_SIZE_OF(task))
  401. return -EPERM;
  402. cpu = get_cpu();
  403. /* handle small bases via the GDT because that's faster to
  404. switch. */
  405. if (addr <= 0xffffffff) {
  406. set_32bit_tls(task, GS_TLS, addr);
  407. if (doit) {
  408. load_TLS(&task->thread, cpu);
  409. load_gs_index(GS_TLS_SEL);
  410. }
  411. task->thread.gsindex = GS_TLS_SEL;
  412. task->thread.gs = 0;
  413. } else {
  414. task->thread.gsindex = 0;
  415. task->thread.gs = addr;
  416. if (doit) {
  417. load_gs_index(0);
  418. ret = checking_wrmsrl(MSR_KERNEL_GS_BASE, addr);
  419. }
  420. }
  421. put_cpu();
  422. break;
  423. case ARCH_SET_FS:
  424. /* Not strictly needed for fs, but do it for symmetry
  425. with gs */
  426. if (addr >= TASK_SIZE_OF(task))
  427. return -EPERM;
  428. cpu = get_cpu();
  429. /* handle small bases via the GDT because that's faster to
  430. switch. */
  431. if (addr <= 0xffffffff) {
  432. set_32bit_tls(task, FS_TLS, addr);
  433. if (doit) {
  434. load_TLS(&task->thread, cpu);
  435. loadsegment(fs, FS_TLS_SEL);
  436. }
  437. task->thread.fsindex = FS_TLS_SEL;
  438. task->thread.fs = 0;
  439. } else {
  440. task->thread.fsindex = 0;
  441. task->thread.fs = addr;
  442. if (doit) {
  443. /* set the selector to 0 to not confuse
  444. __switch_to */
  445. loadsegment(fs, 0);
  446. ret = checking_wrmsrl(MSR_FS_BASE, addr);
  447. }
  448. }
  449. put_cpu();
  450. break;
  451. case ARCH_GET_FS: {
  452. unsigned long base;
  453. if (task->thread.fsindex == FS_TLS_SEL)
  454. base = read_32bit_tls(task, FS_TLS);
  455. else if (doit)
  456. rdmsrl(MSR_FS_BASE, base);
  457. else
  458. base = task->thread.fs;
  459. ret = put_user(base, (unsigned long __user *)addr);
  460. break;
  461. }
  462. case ARCH_GET_GS: {
  463. unsigned long base;
  464. unsigned gsindex;
  465. if (task->thread.gsindex == GS_TLS_SEL)
  466. base = read_32bit_tls(task, GS_TLS);
  467. else if (doit) {
  468. savesegment(gs, gsindex);
  469. if (gsindex)
  470. rdmsrl(MSR_KERNEL_GS_BASE, base);
  471. else
  472. base = task->thread.gs;
  473. } else
  474. base = task->thread.gs;
  475. ret = put_user(base, (unsigned long __user *)addr);
  476. break;
  477. }
  478. default:
  479. ret = -EINVAL;
  480. break;
  481. }
  482. return ret;
  483. }
  484. long sys_arch_prctl(int code, unsigned long addr)
  485. {
  486. return do_arch_prctl(current, code, addr);
  487. }
  488. unsigned long KSTK_ESP(struct task_struct *task)
  489. {
  490. return (test_tsk_thread_flag(task, TIF_IA32)) ?
  491. (task_pt_regs(task)->sp) : ((task)->thread.usersp);
  492. }