processor.h 24 KB

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