processor.h 24 KB

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