processor.h 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. #ifndef _ASM_X86_PROCESSOR_H
  2. #define _ASM_X86_PROCESSOR_H
  3. #include <asm/processor-flags.h>
  4. /* Forward declaration, a strange C thing */
  5. struct task_struct;
  6. struct mm_struct;
  7. #include <asm/vm86.h>
  8. #include <asm/math_emu.h>
  9. #include <asm/segment.h>
  10. #include <asm/types.h>
  11. #include <asm/sigcontext.h>
  12. #include <asm/current.h>
  13. #include <asm/cpufeature.h>
  14. #include <asm/system.h>
  15. #include <asm/page.h>
  16. #include <asm/pgtable_types.h>
  17. #include <asm/percpu.h>
  18. #include <asm/msr.h>
  19. #include <asm/desc_defs.h>
  20. #include <asm/nops.h>
  21. #include <asm/ds.h>
  22. #include <linux/personality.h>
  23. #include <linux/cpumask.h>
  24. #include <linux/cache.h>
  25. #include <linux/threads.h>
  26. #include <linux/math64.h>
  27. #include <linux/init.h>
  28. #define HBP_NUM 4
  29. /*
  30. * Default implementation of macro that returns current
  31. * instruction pointer ("program counter").
  32. */
  33. static inline void *current_text_addr(void)
  34. {
  35. void *pc;
  36. asm volatile("mov $1f, %0; 1:":"=r" (pc));
  37. return pc;
  38. }
  39. #ifdef CONFIG_X86_VSMP
  40. # define ARCH_MIN_TASKALIGN (1 << INTERNODE_CACHE_SHIFT)
  41. # define ARCH_MIN_MMSTRUCT_ALIGN (1 << INTERNODE_CACHE_SHIFT)
  42. #else
  43. # define ARCH_MIN_TASKALIGN 16
  44. # define ARCH_MIN_MMSTRUCT_ALIGN 0
  45. #endif
  46. /*
  47. * CPU type and hardware bug flags. Kept separately for each CPU.
  48. * Members of this structure are referenced in head.S, so think twice
  49. * before touching them. [mj]
  50. */
  51. struct cpuinfo_x86 {
  52. __u8 x86; /* CPU family */
  53. __u8 x86_vendor; /* CPU vendor */
  54. __u8 x86_model;
  55. __u8 x86_mask;
  56. #ifdef CONFIG_X86_32
  57. char wp_works_ok; /* It doesn't on 386's */
  58. /* Problems on some 486Dx4's and old 386's: */
  59. char hlt_works_ok;
  60. char hard_math;
  61. char rfu;
  62. char fdiv_bug;
  63. char f00f_bug;
  64. char coma_bug;
  65. char pad0;
  66. #else
  67. /* Number of 4K pages in DTLB/ITLB combined(in pages): */
  68. int x86_tlbsize;
  69. #endif
  70. __u8 x86_virt_bits;
  71. __u8 x86_phys_bits;
  72. /* CPUID returned core id bits: */
  73. __u8 x86_coreid_bits;
  74. /* Max extended CPUID function supported: */
  75. __u32 extended_cpuid_level;
  76. /* Maximum supported CPUID level, -1=no CPUID: */
  77. int cpuid_level;
  78. __u32 x86_capability[NCAPINTS];
  79. char x86_vendor_id[16];
  80. char x86_model_id[64];
  81. /* in KB - valid for CPUS which support this call: */
  82. int x86_cache_size;
  83. int x86_cache_alignment; /* In bytes */
  84. int x86_power;
  85. unsigned long loops_per_jiffy;
  86. #ifdef CONFIG_SMP
  87. /* cpus sharing the last level cache: */
  88. cpumask_var_t llc_shared_map;
  89. #endif
  90. /* cpuid returned max cores value: */
  91. u16 x86_max_cores;
  92. u16 apicid;
  93. u16 initial_apicid;
  94. u16 x86_clflush_size;
  95. #ifdef CONFIG_SMP
  96. /* number of cores as seen by the OS: */
  97. u16 booted_cores;
  98. /* Physical processor id: */
  99. u16 phys_proc_id;
  100. /* Core id: */
  101. u16 cpu_core_id;
  102. /* Index into per_cpu list: */
  103. u16 cpu_index;
  104. #endif
  105. unsigned int x86_hyper_vendor;
  106. /* The layout of this field is hypervisor specific */
  107. unsigned int x86_hyper_features;
  108. } __attribute__((__aligned__(SMP_CACHE_BYTES)));
  109. #define X86_VENDOR_INTEL 0
  110. #define X86_VENDOR_CYRIX 1
  111. #define X86_VENDOR_AMD 2
  112. #define X86_VENDOR_UMC 3
  113. #define X86_VENDOR_CENTAUR 5
  114. #define X86_VENDOR_TRANSMETA 7
  115. #define X86_VENDOR_NSC 8
  116. #define X86_VENDOR_NUM 9
  117. #define X86_VENDOR_UNKNOWN 0xff
  118. #define X86_HYPER_VENDOR_NONE 0
  119. #define X86_HYPER_VENDOR_VMWARE 1
  120. #define X86_HYPER_VENDOR_MSFT 2
  121. /*
  122. * capabilities of CPUs
  123. */
  124. extern struct cpuinfo_x86 boot_cpu_data;
  125. extern struct cpuinfo_x86 new_cpu_data;
  126. extern struct tss_struct doublefault_tss;
  127. extern __u32 cpu_caps_cleared[NCAPINTS];
  128. extern __u32 cpu_caps_set[NCAPINTS];
  129. #ifdef CONFIG_SMP
  130. DECLARE_PER_CPU_SHARED_ALIGNED(struct cpuinfo_x86, cpu_info);
  131. #define cpu_data(cpu) per_cpu(cpu_info, cpu)
  132. #define current_cpu_data __get_cpu_var(cpu_info)
  133. #else
  134. #define cpu_data(cpu) boot_cpu_data
  135. #define current_cpu_data boot_cpu_data
  136. #endif
  137. extern const struct seq_operations cpuinfo_op;
  138. static inline int hlt_works(int cpu)
  139. {
  140. #ifdef CONFIG_X86_32
  141. return cpu_data(cpu).hlt_works_ok;
  142. #else
  143. return 1;
  144. #endif
  145. }
  146. #define cache_line_size() (boot_cpu_data.x86_cache_alignment)
  147. extern void cpu_detect(struct cpuinfo_x86 *c);
  148. extern struct pt_regs *idle_regs(struct pt_regs *);
  149. extern void early_cpu_init(void);
  150. extern void identify_boot_cpu(void);
  151. extern void identify_secondary_cpu(struct cpuinfo_x86 *);
  152. extern void print_cpu_info(struct cpuinfo_x86 *);
  153. extern void init_scattered_cpuid_features(struct cpuinfo_x86 *c);
  154. extern unsigned int init_intel_cacheinfo(struct cpuinfo_x86 *c);
  155. extern unsigned short num_cache_leaves;
  156. extern void detect_extended_topology(struct cpuinfo_x86 *c);
  157. extern void detect_ht(struct cpuinfo_x86 *c);
  158. static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
  159. unsigned int *ecx, unsigned int *edx)
  160. {
  161. /* ecx is often an input as well as an output. */
  162. asm volatile("cpuid"
  163. : "=a" (*eax),
  164. "=b" (*ebx),
  165. "=c" (*ecx),
  166. "=d" (*edx)
  167. : "0" (*eax), "2" (*ecx));
  168. }
  169. static inline void load_cr3(pgd_t *pgdir)
  170. {
  171. write_cr3(__pa(pgdir));
  172. }
  173. #ifdef CONFIG_X86_32
  174. /* This is the TSS defined by the hardware. */
  175. struct x86_hw_tss {
  176. unsigned short back_link, __blh;
  177. unsigned long sp0;
  178. unsigned short ss0, __ss0h;
  179. unsigned long sp1;
  180. /* ss1 caches MSR_IA32_SYSENTER_CS: */
  181. unsigned short ss1, __ss1h;
  182. unsigned long sp2;
  183. unsigned short ss2, __ss2h;
  184. unsigned long __cr3;
  185. unsigned long ip;
  186. unsigned long flags;
  187. unsigned long ax;
  188. unsigned long cx;
  189. unsigned long dx;
  190. unsigned long bx;
  191. unsigned long sp;
  192. unsigned long bp;
  193. unsigned long si;
  194. unsigned long di;
  195. unsigned short es, __esh;
  196. unsigned short cs, __csh;
  197. unsigned short ss, __ssh;
  198. unsigned short ds, __dsh;
  199. unsigned short fs, __fsh;
  200. unsigned short gs, __gsh;
  201. unsigned short ldt, __ldth;
  202. unsigned short trace;
  203. unsigned short io_bitmap_base;
  204. } __attribute__((packed));
  205. #else
  206. struct x86_hw_tss {
  207. u32 reserved1;
  208. u64 sp0;
  209. u64 sp1;
  210. u64 sp2;
  211. u64 reserved2;
  212. u64 ist[7];
  213. u32 reserved3;
  214. u32 reserved4;
  215. u16 reserved5;
  216. u16 io_bitmap_base;
  217. } __attribute__((packed)) ____cacheline_aligned;
  218. #endif
  219. /*
  220. * IO-bitmap sizes:
  221. */
  222. #define IO_BITMAP_BITS 65536
  223. #define IO_BITMAP_BYTES (IO_BITMAP_BITS/8)
  224. #define IO_BITMAP_LONGS (IO_BITMAP_BYTES/sizeof(long))
  225. #define IO_BITMAP_OFFSET offsetof(struct tss_struct, io_bitmap)
  226. #define INVALID_IO_BITMAP_OFFSET 0x8000
  227. struct tss_struct {
  228. /*
  229. * The hardware state:
  230. */
  231. struct x86_hw_tss x86_tss;
  232. /*
  233. * The extra 1 is there because the CPU will access an
  234. * additional byte beyond the end of the IO permission
  235. * bitmap. The extra byte must be all 1 bits, and must
  236. * be within the limit.
  237. */
  238. unsigned long io_bitmap[IO_BITMAP_LONGS + 1];
  239. /*
  240. * .. and then another 0x100 bytes for the emergency kernel stack:
  241. */
  242. unsigned long stack[64];
  243. } ____cacheline_aligned;
  244. DECLARE_PER_CPU_SHARED_ALIGNED(struct tss_struct, init_tss);
  245. /*
  246. * Save the original ist values for checking stack pointers during debugging
  247. */
  248. struct orig_ist {
  249. unsigned long ist[7];
  250. };
  251. #define MXCSR_DEFAULT 0x1f80
  252. struct i387_fsave_struct {
  253. u32 cwd; /* FPU Control Word */
  254. u32 swd; /* FPU Status Word */
  255. u32 twd; /* FPU Tag Word */
  256. u32 fip; /* FPU IP Offset */
  257. u32 fcs; /* FPU IP Selector */
  258. u32 foo; /* FPU Operand Pointer Offset */
  259. u32 fos; /* FPU Operand Pointer Selector */
  260. /* 8*10 bytes for each FP-reg = 80 bytes: */
  261. u32 st_space[20];
  262. /* Software status information [not touched by FSAVE ]: */
  263. u32 status;
  264. };
  265. struct i387_fxsave_struct {
  266. u16 cwd; /* Control Word */
  267. u16 swd; /* Status Word */
  268. u16 twd; /* Tag Word */
  269. u16 fop; /* Last Instruction Opcode */
  270. union {
  271. struct {
  272. u64 rip; /* Instruction Pointer */
  273. u64 rdp; /* Data Pointer */
  274. };
  275. struct {
  276. u32 fip; /* FPU IP Offset */
  277. u32 fcs; /* FPU IP Selector */
  278. u32 foo; /* FPU Operand Offset */
  279. u32 fos; /* FPU Operand Selector */
  280. };
  281. };
  282. u32 mxcsr; /* MXCSR Register State */
  283. u32 mxcsr_mask; /* MXCSR Mask */
  284. /* 8*16 bytes for each FP-reg = 128 bytes: */
  285. u32 st_space[32];
  286. /* 16*16 bytes for each XMM-reg = 256 bytes: */
  287. u32 xmm_space[64];
  288. u32 padding[12];
  289. union {
  290. u32 padding1[12];
  291. u32 sw_reserved[12];
  292. };
  293. } __attribute__((aligned(16)));
  294. struct i387_soft_struct {
  295. u32 cwd;
  296. u32 swd;
  297. u32 twd;
  298. u32 fip;
  299. u32 fcs;
  300. u32 foo;
  301. u32 fos;
  302. /* 8*10 bytes for each FP-reg = 80 bytes: */
  303. u32 st_space[20];
  304. u8 ftop;
  305. u8 changed;
  306. u8 lookahead;
  307. u8 no_update;
  308. u8 rm;
  309. u8 alimit;
  310. struct math_emu_info *info;
  311. u32 entry_eip;
  312. };
  313. struct ymmh_struct {
  314. /* 16 * 16 bytes for each YMMH-reg = 256 bytes */
  315. u32 ymmh_space[64];
  316. };
  317. struct xsave_hdr_struct {
  318. u64 xstate_bv;
  319. u64 reserved1[2];
  320. u64 reserved2[5];
  321. } __attribute__((packed));
  322. struct xsave_struct {
  323. struct i387_fxsave_struct i387;
  324. struct xsave_hdr_struct xsave_hdr;
  325. struct ymmh_struct ymmh;
  326. /* new processor state extensions will go here */
  327. } __attribute__ ((packed, aligned (64)));
  328. union thread_xstate {
  329. struct i387_fsave_struct fsave;
  330. struct i387_fxsave_struct fxsave;
  331. struct i387_soft_struct soft;
  332. struct xsave_struct xsave;
  333. };
  334. #ifdef CONFIG_X86_64
  335. DECLARE_PER_CPU(struct orig_ist, orig_ist);
  336. union irq_stack_union {
  337. char irq_stack[IRQ_STACK_SIZE];
  338. /*
  339. * GCC hardcodes the stack canary as %gs:40. Since the
  340. * irq_stack is the object at %gs:0, we reserve the bottom
  341. * 48 bytes of the irq stack for the canary.
  342. */
  343. struct {
  344. char gs_base[40];
  345. unsigned long stack_canary;
  346. };
  347. };
  348. DECLARE_PER_CPU_FIRST(union irq_stack_union, irq_stack_union);
  349. DECLARE_INIT_PER_CPU(irq_stack_union);
  350. DECLARE_PER_CPU(char *, irq_stack_ptr);
  351. DECLARE_PER_CPU(unsigned int, irq_count);
  352. extern unsigned long kernel_eflags;
  353. extern asmlinkage void ignore_sysret(void);
  354. #else /* X86_64 */
  355. #ifdef CONFIG_CC_STACKPROTECTOR
  356. /*
  357. * Make sure stack canary segment base is cached-aligned:
  358. * "For Intel Atom processors, avoid non zero segment base address
  359. * that is not aligned to cache line boundary at all cost."
  360. * (Optim Ref Manual Assembly/Compiler Coding Rule 15.)
  361. */
  362. struct stack_canary {
  363. char __pad[20]; /* canary at %gs:20 */
  364. unsigned long canary;
  365. };
  366. DECLARE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
  367. #endif
  368. #endif /* X86_64 */
  369. extern unsigned int xstate_size;
  370. extern void free_thread_xstate(struct task_struct *);
  371. extern struct kmem_cache *task_xstate_cachep;
  372. struct perf_event;
  373. struct thread_struct {
  374. /* Cached TLS descriptors: */
  375. struct desc_struct tls_array[GDT_ENTRY_TLS_ENTRIES];
  376. unsigned long sp0;
  377. unsigned long sp;
  378. #ifdef CONFIG_X86_32
  379. unsigned long sysenter_cs;
  380. #else
  381. unsigned long usersp; /* Copy from PDA */
  382. unsigned short es;
  383. unsigned short ds;
  384. unsigned short fsindex;
  385. unsigned short gsindex;
  386. #endif
  387. #ifdef CONFIG_X86_32
  388. unsigned long ip;
  389. #endif
  390. #ifdef CONFIG_X86_64
  391. unsigned long fs;
  392. #endif
  393. unsigned long gs;
  394. /* Save middle states of ptrace breakpoints */
  395. struct perf_event *ptrace_bps[HBP_NUM];
  396. /* Debug status used for traps, single steps, etc... */
  397. unsigned long debugreg6;
  398. /* Keep track of the exact dr7 value set by the user */
  399. unsigned long ptrace_dr7;
  400. /* Fault info: */
  401. unsigned long cr2;
  402. unsigned long trap_no;
  403. unsigned long error_code;
  404. /* floating point and extended processor state */
  405. union thread_xstate *xstate;
  406. #ifdef CONFIG_X86_32
  407. /* Virtual 86 mode info */
  408. struct vm86_struct __user *vm86_info;
  409. unsigned long screen_bitmap;
  410. unsigned long v86flags;
  411. unsigned long v86mask;
  412. unsigned long saved_sp0;
  413. unsigned int saved_fs;
  414. unsigned int saved_gs;
  415. #endif
  416. /* IO permissions: */
  417. unsigned long *io_bitmap_ptr;
  418. unsigned long iopl;
  419. /* Max allowed port in the bitmap, in bytes: */
  420. unsigned io_bitmap_max;
  421. /* MSR_IA32_DEBUGCTLMSR value to switch in if TIF_DEBUGCTLMSR is set. */
  422. unsigned long debugctlmsr;
  423. /* Debug Store context; see asm/ds.h */
  424. struct ds_context *ds_ctx;
  425. };
  426. static inline unsigned long native_get_debugreg(int regno)
  427. {
  428. unsigned long val = 0; /* Damn you, gcc! */
  429. switch (regno) {
  430. case 0:
  431. asm("mov %%db0, %0" :"=r" (val));
  432. break;
  433. case 1:
  434. asm("mov %%db1, %0" :"=r" (val));
  435. break;
  436. case 2:
  437. asm("mov %%db2, %0" :"=r" (val));
  438. break;
  439. case 3:
  440. asm("mov %%db3, %0" :"=r" (val));
  441. break;
  442. case 6:
  443. asm("mov %%db6, %0" :"=r" (val));
  444. break;
  445. case 7:
  446. asm("mov %%db7, %0" :"=r" (val));
  447. break;
  448. default:
  449. BUG();
  450. }
  451. return val;
  452. }
  453. static inline void native_set_debugreg(int regno, unsigned long value)
  454. {
  455. switch (regno) {
  456. case 0:
  457. asm("mov %0, %%db0" ::"r" (value));
  458. break;
  459. case 1:
  460. asm("mov %0, %%db1" ::"r" (value));
  461. break;
  462. case 2:
  463. asm("mov %0, %%db2" ::"r" (value));
  464. break;
  465. case 3:
  466. asm("mov %0, %%db3" ::"r" (value));
  467. break;
  468. case 6:
  469. asm("mov %0, %%db6" ::"r" (value));
  470. break;
  471. case 7:
  472. asm("mov %0, %%db7" ::"r" (value));
  473. break;
  474. default:
  475. BUG();
  476. }
  477. }
  478. /*
  479. * Set IOPL bits in EFLAGS from given mask
  480. */
  481. static inline void native_set_iopl_mask(unsigned mask)
  482. {
  483. #ifdef CONFIG_X86_32
  484. unsigned int reg;
  485. asm volatile ("pushfl;"
  486. "popl %0;"
  487. "andl %1, %0;"
  488. "orl %2, %0;"
  489. "pushl %0;"
  490. "popfl"
  491. : "=&r" (reg)
  492. : "i" (~X86_EFLAGS_IOPL), "r" (mask));
  493. #endif
  494. }
  495. static inline void
  496. native_load_sp0(struct tss_struct *tss, struct thread_struct *thread)
  497. {
  498. tss->x86_tss.sp0 = thread->sp0;
  499. #ifdef CONFIG_X86_32
  500. /* Only happens when SEP is enabled, no need to test "SEP"arately: */
  501. if (unlikely(tss->x86_tss.ss1 != thread->sysenter_cs)) {
  502. tss->x86_tss.ss1 = thread->sysenter_cs;
  503. wrmsr(MSR_IA32_SYSENTER_CS, thread->sysenter_cs, 0);
  504. }
  505. #endif
  506. }
  507. static inline void native_swapgs(void)
  508. {
  509. #ifdef CONFIG_X86_64
  510. asm volatile("swapgs" ::: "memory");
  511. #endif
  512. }
  513. #ifdef CONFIG_PARAVIRT
  514. #include <asm/paravirt.h>
  515. #else
  516. #define __cpuid native_cpuid
  517. #define paravirt_enabled() 0
  518. /*
  519. * These special macros can be used to get or set a debugging register
  520. */
  521. #define get_debugreg(var, register) \
  522. (var) = native_get_debugreg(register)
  523. #define set_debugreg(value, register) \
  524. native_set_debugreg(register, value)
  525. static inline void load_sp0(struct tss_struct *tss,
  526. struct thread_struct *thread)
  527. {
  528. native_load_sp0(tss, thread);
  529. }
  530. #define set_iopl_mask native_set_iopl_mask
  531. #endif /* CONFIG_PARAVIRT */
  532. /*
  533. * Save the cr4 feature set we're using (ie
  534. * Pentium 4MB enable and PPro Global page
  535. * enable), so that any CPU's that boot up
  536. * after us can get the correct flags.
  537. */
  538. extern unsigned long mmu_cr4_features;
  539. static inline void set_in_cr4(unsigned long mask)
  540. {
  541. unsigned cr4;
  542. mmu_cr4_features |= mask;
  543. cr4 = read_cr4();
  544. cr4 |= mask;
  545. write_cr4(cr4);
  546. }
  547. static inline void clear_in_cr4(unsigned long mask)
  548. {
  549. unsigned cr4;
  550. mmu_cr4_features &= ~mask;
  551. cr4 = read_cr4();
  552. cr4 &= ~mask;
  553. write_cr4(cr4);
  554. }
  555. typedef struct {
  556. unsigned long seg;
  557. } mm_segment_t;
  558. /*
  559. * create a kernel thread without removing it from tasklists
  560. */
  561. extern int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags);
  562. /* Free all resources held by a thread. */
  563. extern void release_thread(struct task_struct *);
  564. /* Prepare to copy thread state - unlazy all lazy state */
  565. extern void prepare_to_copy(struct task_struct *tsk);
  566. unsigned long get_wchan(struct task_struct *p);
  567. /*
  568. * Generic CPUID function
  569. * clear %ecx since some cpus (Cyrix MII) do not set or clear %ecx
  570. * resulting in stale register contents being returned.
  571. */
  572. static inline void cpuid(unsigned int op,
  573. unsigned int *eax, unsigned int *ebx,
  574. unsigned int *ecx, unsigned int *edx)
  575. {
  576. *eax = op;
  577. *ecx = 0;
  578. __cpuid(eax, ebx, ecx, edx);
  579. }
  580. /* Some CPUID calls want 'count' to be placed in ecx */
  581. static inline void cpuid_count(unsigned int op, int count,
  582. unsigned int *eax, unsigned int *ebx,
  583. unsigned int *ecx, unsigned int *edx)
  584. {
  585. *eax = op;
  586. *ecx = count;
  587. __cpuid(eax, ebx, ecx, edx);
  588. }
  589. /*
  590. * CPUID functions returning a single datum
  591. */
  592. static inline unsigned int cpuid_eax(unsigned int op)
  593. {
  594. unsigned int eax, ebx, ecx, edx;
  595. cpuid(op, &eax, &ebx, &ecx, &edx);
  596. return eax;
  597. }
  598. static inline unsigned int cpuid_ebx(unsigned int op)
  599. {
  600. unsigned int eax, ebx, ecx, edx;
  601. cpuid(op, &eax, &ebx, &ecx, &edx);
  602. return ebx;
  603. }
  604. static inline unsigned int cpuid_ecx(unsigned int op)
  605. {
  606. unsigned int eax, ebx, ecx, edx;
  607. cpuid(op, &eax, &ebx, &ecx, &edx);
  608. return ecx;
  609. }
  610. static inline unsigned int cpuid_edx(unsigned int op)
  611. {
  612. unsigned int eax, ebx, ecx, edx;
  613. cpuid(op, &eax, &ebx, &ecx, &edx);
  614. return edx;
  615. }
  616. /* REP NOP (PAUSE) is a good thing to insert into busy-wait loops. */
  617. static inline void rep_nop(void)
  618. {
  619. asm volatile("rep; nop" ::: "memory");
  620. }
  621. static inline void cpu_relax(void)
  622. {
  623. rep_nop();
  624. }
  625. /* Stop speculative execution and prefetching of modified code. */
  626. static inline void sync_core(void)
  627. {
  628. int tmp;
  629. #if defined(CONFIG_M386) || defined(CONFIG_M486)
  630. if (boot_cpu_data.x86 < 5)
  631. /* There is no speculative execution.
  632. * jmp is a barrier to prefetching. */
  633. asm volatile("jmp 1f\n1:\n" ::: "memory");
  634. else
  635. #endif
  636. /* cpuid is a barrier to speculative execution.
  637. * Prefetched instructions are automatically
  638. * invalidated when modified. */
  639. asm volatile("cpuid" : "=a" (tmp) : "0" (1)
  640. : "ebx", "ecx", "edx", "memory");
  641. }
  642. static inline void __monitor(const void *eax, unsigned long ecx,
  643. unsigned long edx)
  644. {
  645. /* "monitor %eax, %ecx, %edx;" */
  646. asm volatile(".byte 0x0f, 0x01, 0xc8;"
  647. :: "a" (eax), "c" (ecx), "d"(edx));
  648. }
  649. static inline void __mwait(unsigned long eax, unsigned long ecx)
  650. {
  651. /* "mwait %eax, %ecx;" */
  652. asm volatile(".byte 0x0f, 0x01, 0xc9;"
  653. :: "a" (eax), "c" (ecx));
  654. }
  655. static inline void __sti_mwait(unsigned long eax, unsigned long ecx)
  656. {
  657. trace_hardirqs_on();
  658. /* "mwait %eax, %ecx;" */
  659. asm volatile("sti; .byte 0x0f, 0x01, 0xc9;"
  660. :: "a" (eax), "c" (ecx));
  661. }
  662. extern void mwait_idle_with_hints(unsigned long eax, unsigned long ecx);
  663. extern void select_idle_routine(const struct cpuinfo_x86 *c);
  664. extern void init_c1e_mask(void);
  665. extern unsigned long boot_option_idle_override;
  666. extern unsigned long idle_halt;
  667. extern unsigned long idle_nomwait;
  668. /*
  669. * on systems with caches, caches must be flashed as the absolute
  670. * last instruction before going into a suspended halt. Otherwise,
  671. * dirty data can linger in the cache and become stale on resume,
  672. * leading to strange errors.
  673. *
  674. * perform a variety of operations to guarantee that the compiler
  675. * will not reorder instructions. wbinvd itself is serializing
  676. * so the processor will not reorder.
  677. *
  678. * Systems without cache can just go into halt.
  679. */
  680. static inline void wbinvd_halt(void)
  681. {
  682. mb();
  683. /* check for clflush to determine if wbinvd is legal */
  684. if (cpu_has_clflush)
  685. asm volatile("cli; wbinvd; 1: hlt; jmp 1b" : : : "memory");
  686. else
  687. while (1)
  688. halt();
  689. }
  690. extern void enable_sep_cpu(void);
  691. extern int sysenter_setup(void);
  692. /* Defined in head.S */
  693. extern struct desc_ptr early_gdt_descr;
  694. extern void cpu_set_gdt(int);
  695. extern void switch_to_new_gdt(int);
  696. extern void load_percpu_segment(int);
  697. extern void cpu_init(void);
  698. static inline unsigned long get_debugctlmsr(void)
  699. {
  700. unsigned long debugctlmsr = 0;
  701. #ifndef CONFIG_X86_DEBUGCTLMSR
  702. if (boot_cpu_data.x86 < 6)
  703. return 0;
  704. #endif
  705. rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctlmsr);
  706. return debugctlmsr;
  707. }
  708. static inline unsigned long get_debugctlmsr_on_cpu(int cpu)
  709. {
  710. u64 debugctlmsr = 0;
  711. u32 val1, val2;
  712. #ifndef CONFIG_X86_DEBUGCTLMSR
  713. if (boot_cpu_data.x86 < 6)
  714. return 0;
  715. #endif
  716. rdmsr_on_cpu(cpu, MSR_IA32_DEBUGCTLMSR, &val1, &val2);
  717. debugctlmsr = val1 | ((u64)val2 << 32);
  718. return debugctlmsr;
  719. }
  720. static inline void update_debugctlmsr(unsigned long debugctlmsr)
  721. {
  722. #ifndef CONFIG_X86_DEBUGCTLMSR
  723. if (boot_cpu_data.x86 < 6)
  724. return;
  725. #endif
  726. wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctlmsr);
  727. }
  728. static inline void update_debugctlmsr_on_cpu(int cpu,
  729. unsigned long debugctlmsr)
  730. {
  731. #ifndef CONFIG_X86_DEBUGCTLMSR
  732. if (boot_cpu_data.x86 < 6)
  733. return;
  734. #endif
  735. wrmsr_on_cpu(cpu, MSR_IA32_DEBUGCTLMSR,
  736. (u32)((u64)debugctlmsr),
  737. (u32)((u64)debugctlmsr >> 32));
  738. }
  739. /*
  740. * from system description table in BIOS. Mostly for MCA use, but
  741. * others may find it useful:
  742. */
  743. extern unsigned int machine_id;
  744. extern unsigned int machine_submodel_id;
  745. extern unsigned int BIOS_revision;
  746. /* Boot loader type from the setup header: */
  747. extern int bootloader_type;
  748. extern int bootloader_version;
  749. extern char ignore_fpu_irq;
  750. #define HAVE_ARCH_PICK_MMAP_LAYOUT 1
  751. #define ARCH_HAS_PREFETCHW
  752. #define ARCH_HAS_SPINLOCK_PREFETCH
  753. #ifdef CONFIG_X86_32
  754. # define BASE_PREFETCH ASM_NOP4
  755. # define ARCH_HAS_PREFETCH
  756. #else
  757. # define BASE_PREFETCH "prefetcht0 (%1)"
  758. #endif
  759. /*
  760. * Prefetch instructions for Pentium III (+) and AMD Athlon (+)
  761. *
  762. * It's not worth to care about 3dnow prefetches for the K6
  763. * because they are microcoded there and very slow.
  764. */
  765. static inline void prefetch(const void *x)
  766. {
  767. alternative_input(BASE_PREFETCH,
  768. "prefetchnta (%1)",
  769. X86_FEATURE_XMM,
  770. "r" (x));
  771. }
  772. /*
  773. * 3dnow prefetch to get an exclusive cache line.
  774. * Useful for spinlocks to avoid one state transition in the
  775. * cache coherency protocol:
  776. */
  777. static inline void prefetchw(const void *x)
  778. {
  779. alternative_input(BASE_PREFETCH,
  780. "prefetchw (%1)",
  781. X86_FEATURE_3DNOW,
  782. "r" (x));
  783. }
  784. static inline void spin_lock_prefetch(const void *x)
  785. {
  786. prefetchw(x);
  787. }
  788. #ifdef CONFIG_X86_32
  789. /*
  790. * User space process size: 3GB (default).
  791. */
  792. #define TASK_SIZE PAGE_OFFSET
  793. #define TASK_SIZE_MAX TASK_SIZE
  794. #define STACK_TOP TASK_SIZE
  795. #define STACK_TOP_MAX STACK_TOP
  796. #define INIT_THREAD { \
  797. .sp0 = sizeof(init_stack) + (long)&init_stack, \
  798. .vm86_info = NULL, \
  799. .sysenter_cs = __KERNEL_CS, \
  800. .io_bitmap_ptr = NULL, \
  801. }
  802. /*
  803. * Note that the .io_bitmap member must be extra-big. This is because
  804. * the CPU will access an additional byte beyond the end of the IO
  805. * permission bitmap. The extra byte must be all 1 bits, and must
  806. * be within the limit.
  807. */
  808. #define INIT_TSS { \
  809. .x86_tss = { \
  810. .sp0 = sizeof(init_stack) + (long)&init_stack, \
  811. .ss0 = __KERNEL_DS, \
  812. .ss1 = __KERNEL_CS, \
  813. .io_bitmap_base = INVALID_IO_BITMAP_OFFSET, \
  814. }, \
  815. .io_bitmap = { [0 ... IO_BITMAP_LONGS] = ~0 }, \
  816. }
  817. extern unsigned long thread_saved_pc(struct task_struct *tsk);
  818. #define THREAD_SIZE_LONGS (THREAD_SIZE/sizeof(unsigned long))
  819. #define KSTK_TOP(info) \
  820. ({ \
  821. unsigned long *__ptr = (unsigned long *)(info); \
  822. (unsigned long)(&__ptr[THREAD_SIZE_LONGS]); \
  823. })
  824. /*
  825. * The below -8 is to reserve 8 bytes on top of the ring0 stack.
  826. * This is necessary to guarantee that the entire "struct pt_regs"
  827. * is accessable even if the CPU haven't stored the SS/ESP registers
  828. * on the stack (interrupt gate does not save these registers
  829. * when switching to the same priv ring).
  830. * Therefore beware: accessing the ss/esp fields of the
  831. * "struct pt_regs" is possible, but they may contain the
  832. * completely wrong values.
  833. */
  834. #define task_pt_regs(task) \
  835. ({ \
  836. struct pt_regs *__regs__; \
  837. __regs__ = (struct pt_regs *)(KSTK_TOP(task_stack_page(task))-8); \
  838. __regs__ - 1; \
  839. })
  840. #define KSTK_ESP(task) (task_pt_regs(task)->sp)
  841. #else
  842. /*
  843. * User space process size. 47bits minus one guard page.
  844. */
  845. #define TASK_SIZE_MAX ((1UL << 47) - PAGE_SIZE)
  846. /* This decides where the kernel will search for a free chunk of vm
  847. * space during mmap's.
  848. */
  849. #define IA32_PAGE_OFFSET ((current->personality & ADDR_LIMIT_3GB) ? \
  850. 0xc0000000 : 0xFFFFe000)
  851. #define TASK_SIZE (test_thread_flag(TIF_IA32) ? \
  852. IA32_PAGE_OFFSET : TASK_SIZE_MAX)
  853. #define TASK_SIZE_OF(child) ((test_tsk_thread_flag(child, TIF_IA32)) ? \
  854. IA32_PAGE_OFFSET : TASK_SIZE_MAX)
  855. #define STACK_TOP TASK_SIZE
  856. #define STACK_TOP_MAX TASK_SIZE_MAX
  857. #define INIT_THREAD { \
  858. .sp0 = (unsigned long)&init_stack + sizeof(init_stack) \
  859. }
  860. #define INIT_TSS { \
  861. .x86_tss.sp0 = (unsigned long)&init_stack + sizeof(init_stack) \
  862. }
  863. /*
  864. * Return saved PC of a blocked thread.
  865. * What is this good for? it will be always the scheduler or ret_from_fork.
  866. */
  867. #define thread_saved_pc(t) (*(unsigned long *)((t)->thread.sp - 8))
  868. #define task_pt_regs(tsk) ((struct pt_regs *)(tsk)->thread.sp0 - 1)
  869. extern unsigned long KSTK_ESP(struct task_struct *task);
  870. #endif /* CONFIG_X86_64 */
  871. extern void start_thread(struct pt_regs *regs, unsigned long new_ip,
  872. unsigned long new_sp);
  873. /*
  874. * This decides where the kernel will search for a free chunk of vm
  875. * space during mmap's.
  876. */
  877. #define TASK_UNMAPPED_BASE (PAGE_ALIGN(TASK_SIZE / 3))
  878. #define KSTK_EIP(task) (task_pt_regs(task)->ip)
  879. /* Get/set a process' ability to use the timestamp counter instruction */
  880. #define GET_TSC_CTL(adr) get_tsc_mode((adr))
  881. #define SET_TSC_CTL(val) set_tsc_mode((val))
  882. extern int get_tsc_mode(unsigned long adr);
  883. extern int set_tsc_mode(unsigned int val);
  884. extern int amd_get_nb_id(int cpu);
  885. struct aperfmperf {
  886. u64 aperf, mperf;
  887. };
  888. static inline void get_aperfmperf(struct aperfmperf *am)
  889. {
  890. WARN_ON_ONCE(!boot_cpu_has(X86_FEATURE_APERFMPERF));
  891. rdmsrl(MSR_IA32_APERF, am->aperf);
  892. rdmsrl(MSR_IA32_MPERF, am->mperf);
  893. }
  894. #define APERFMPERF_SHIFT 10
  895. static inline
  896. unsigned long calc_aperfmperf_ratio(struct aperfmperf *old,
  897. struct aperfmperf *new)
  898. {
  899. u64 aperf = new->aperf - old->aperf;
  900. u64 mperf = new->mperf - old->mperf;
  901. unsigned long ratio = aperf;
  902. mperf >>= APERFMPERF_SHIFT;
  903. if (mperf)
  904. ratio = div64_u64(aperf, mperf);
  905. return ratio;
  906. }
  907. #endif /* _ASM_X86_PROCESSOR_H */