processor.h 24 KB

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