processor.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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/page.h>
  8. #include <asm/percpu.h>
  9. #include <asm/system.h>
  10. #include <asm/percpu.h>
  11. #include <linux/cpumask.h>
  12. #include <linux/cache.h>
  13. /*
  14. * Default implementation of macro that returns current
  15. * instruction pointer ("program counter").
  16. */
  17. static inline void *current_text_addr(void)
  18. {
  19. void *pc;
  20. asm volatile("mov $1f,%0\n1:":"=r" (pc));
  21. return pc;
  22. }
  23. #ifdef CONFIG_X86_VSMP
  24. #define ARCH_MIN_TASKALIGN (1 << INTERNODE_CACHE_SHIFT)
  25. #define ARCH_MIN_MMSTRUCT_ALIGN (1 << INTERNODE_CACHE_SHIFT)
  26. #else
  27. #define ARCH_MIN_TASKALIGN 16
  28. #define ARCH_MIN_MMSTRUCT_ALIGN 0
  29. #endif
  30. /*
  31. * CPU type and hardware bug flags. Kept separately for each CPU.
  32. * Members of this structure are referenced in head.S, so think twice
  33. * before touching them. [mj]
  34. */
  35. struct cpuinfo_x86 {
  36. __u8 x86; /* CPU family */
  37. __u8 x86_vendor; /* CPU vendor */
  38. __u8 x86_model;
  39. __u8 x86_mask;
  40. #ifdef CONFIG_X86_32
  41. char wp_works_ok; /* It doesn't on 386's */
  42. char hlt_works_ok; /* Problems on some 486Dx4's and old 386's */
  43. char hard_math;
  44. char rfu;
  45. char fdiv_bug;
  46. char f00f_bug;
  47. char coma_bug;
  48. char pad0;
  49. #else
  50. /* number of 4K pages in DTLB/ITLB combined(in pages)*/
  51. int x86_tlbsize;
  52. __u8 x86_virt_bits, x86_phys_bits;
  53. /* cpuid returned core id bits */
  54. __u8 x86_coreid_bits;
  55. /* Max extended CPUID function supported */
  56. __u32 extended_cpuid_level;
  57. #endif
  58. int cpuid_level; /* Maximum supported CPUID level, -1=no CPUID */
  59. __u32 x86_capability[NCAPINTS];
  60. char x86_vendor_id[16];
  61. char x86_model_id[64];
  62. int x86_cache_size; /* in KB - valid for CPUS which support this
  63. call */
  64. int x86_cache_alignment; /* In bytes */
  65. int x86_power;
  66. unsigned long loops_per_jiffy;
  67. #ifdef CONFIG_SMP
  68. cpumask_t llc_shared_map; /* cpus sharing the last level cache */
  69. #endif
  70. unsigned char x86_max_cores; /* cpuid returned max cores value */
  71. unsigned char apicid;
  72. unsigned short x86_clflush_size;
  73. #ifdef CONFIG_SMP
  74. unsigned char booted_cores; /* number of cores as seen by OS */
  75. __u8 phys_proc_id; /* Physical processor id. */
  76. __u8 cpu_core_id; /* Core id */
  77. __u8 cpu_index; /* index into per_cpu list */
  78. #endif
  79. } __attribute__((__aligned__(SMP_CACHE_BYTES)));
  80. #define X86_VENDOR_INTEL 0
  81. #define X86_VENDOR_CYRIX 1
  82. #define X86_VENDOR_AMD 2
  83. #define X86_VENDOR_UMC 3
  84. #define X86_VENDOR_NEXGEN 4
  85. #define X86_VENDOR_CENTAUR 5
  86. #define X86_VENDOR_TRANSMETA 7
  87. #define X86_VENDOR_NSC 8
  88. #define X86_VENDOR_NUM 9
  89. #define X86_VENDOR_UNKNOWN 0xff
  90. /*
  91. * capabilities of CPUs
  92. */
  93. extern struct cpuinfo_x86 boot_cpu_data;
  94. extern struct cpuinfo_x86 new_cpu_data;
  95. extern struct tss_struct doublefault_tss;
  96. #ifdef CONFIG_SMP
  97. DECLARE_PER_CPU(struct cpuinfo_x86, cpu_info);
  98. #define cpu_data(cpu) per_cpu(cpu_info, cpu)
  99. #define current_cpu_data cpu_data(smp_processor_id())
  100. #else
  101. #define cpu_data(cpu) boot_cpu_data
  102. #define current_cpu_data boot_cpu_data
  103. #endif
  104. void cpu_detect(struct cpuinfo_x86 *c);
  105. extern void identify_cpu(struct cpuinfo_x86 *);
  106. extern void identify_boot_cpu(void);
  107. extern void identify_secondary_cpu(struct cpuinfo_x86 *);
  108. extern void print_cpu_info(struct cpuinfo_x86 *);
  109. extern void init_scattered_cpuid_features(struct cpuinfo_x86 *c);
  110. extern unsigned int init_intel_cacheinfo(struct cpuinfo_x86 *c);
  111. extern unsigned short num_cache_leaves;
  112. #if defined(CONFIG_X86_HT) || defined(CONFIG_X86_64)
  113. extern void detect_ht(struct cpuinfo_x86 *c);
  114. #else
  115. static inline void detect_ht(struct cpuinfo_x86 *c) {}
  116. #endif
  117. static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
  118. unsigned int *ecx, unsigned int *edx)
  119. {
  120. /* ecx is often an input as well as an output. */
  121. __asm__("cpuid"
  122. : "=a" (*eax),
  123. "=b" (*ebx),
  124. "=c" (*ecx),
  125. "=d" (*edx)
  126. : "0" (*eax), "2" (*ecx));
  127. }
  128. static inline void load_cr3(pgd_t *pgdir)
  129. {
  130. write_cr3(__pa(pgdir));
  131. }
  132. #ifdef CONFIG_X86_32
  133. /* This is the TSS defined by the hardware. */
  134. struct x86_hw_tss {
  135. unsigned short back_link, __blh;
  136. unsigned long sp0;
  137. unsigned short ss0, __ss0h;
  138. unsigned long sp1;
  139. unsigned short ss1, __ss1h; /* ss1 caches MSR_IA32_SYSENTER_CS */
  140. unsigned long sp2;
  141. unsigned short ss2, __ss2h;
  142. unsigned long __cr3;
  143. unsigned long ip;
  144. unsigned long flags;
  145. unsigned long ax, cx, dx, bx;
  146. unsigned long sp, bp, si, di;
  147. unsigned short es, __esh;
  148. unsigned short cs, __csh;
  149. unsigned short ss, __ssh;
  150. unsigned short ds, __dsh;
  151. unsigned short fs, __fsh;
  152. unsigned short gs, __gsh;
  153. unsigned short ldt, __ldth;
  154. unsigned short trace, io_bitmap_base;
  155. } __attribute__((packed));
  156. #else
  157. struct x86_hw_tss {
  158. u32 reserved1;
  159. u64 sp0;
  160. u64 sp1;
  161. u64 sp2;
  162. u64 reserved2;
  163. u64 ist[7];
  164. u32 reserved3;
  165. u32 reserved4;
  166. u16 reserved5;
  167. u16 io_bitmap_base;
  168. } __attribute__((packed)) ____cacheline_aligned;
  169. #endif
  170. /*
  171. * Size of io_bitmap.
  172. */
  173. #define IO_BITMAP_BITS 65536
  174. #define IO_BITMAP_BYTES (IO_BITMAP_BITS/8)
  175. #define IO_BITMAP_LONGS (IO_BITMAP_BYTES/sizeof(long))
  176. #define IO_BITMAP_OFFSET offsetof(struct tss_struct, io_bitmap)
  177. #define INVALID_IO_BITMAP_OFFSET 0x8000
  178. #define INVALID_IO_BITMAP_OFFSET_LAZY 0x9000
  179. struct tss_struct {
  180. struct x86_hw_tss x86_tss;
  181. /*
  182. * The extra 1 is there because the CPU will access an
  183. * additional byte beyond the end of the IO permission
  184. * bitmap. The extra byte must be all 1 bits, and must
  185. * be within the limit.
  186. */
  187. unsigned long io_bitmap[IO_BITMAP_LONGS + 1];
  188. /*
  189. * Cache the current maximum and the last task that used the bitmap:
  190. */
  191. unsigned long io_bitmap_max;
  192. struct thread_struct *io_bitmap_owner;
  193. /*
  194. * pads the TSS to be cacheline-aligned (size is 0x100)
  195. */
  196. unsigned long __cacheline_filler[35];
  197. /*
  198. * .. and then another 0x100 bytes for emergency kernel stack
  199. */
  200. unsigned long stack[64];
  201. } __attribute__((packed));
  202. DECLARE_PER_CPU(struct tss_struct, init_tss);
  203. /* Save the original ist values for checking stack pointers during debugging */
  204. struct orig_ist {
  205. unsigned long ist[7];
  206. };
  207. #ifdef CONFIG_X86_32
  208. # include "processor_32.h"
  209. #else
  210. # include "processor_64.h"
  211. #endif
  212. extern void print_cpu_info(struct cpuinfo_x86 *);
  213. extern void init_scattered_cpuid_features(struct cpuinfo_x86 *c);
  214. extern unsigned int init_intel_cacheinfo(struct cpuinfo_x86 *c);
  215. extern unsigned short num_cache_leaves;
  216. struct thread_struct {
  217. /* cached TLS descriptors. */
  218. struct desc_struct tls_array[GDT_ENTRY_TLS_ENTRIES];
  219. unsigned long sp0;
  220. unsigned long sp;
  221. #ifdef CONFIG_X86_32
  222. unsigned long sysenter_cs;
  223. #else
  224. unsigned long usersp; /* Copy from PDA */
  225. unsigned short es, ds, fsindex, gsindex;
  226. #endif
  227. unsigned long ip;
  228. unsigned long fs;
  229. unsigned long gs;
  230. /* Hardware debugging registers */
  231. unsigned long debugreg0;
  232. unsigned long debugreg1;
  233. unsigned long debugreg2;
  234. unsigned long debugreg3;
  235. unsigned long debugreg6;
  236. unsigned long debugreg7;
  237. /* fault info */
  238. unsigned long cr2, trap_no, error_code;
  239. /* floating point info */
  240. union i387_union i387 __attribute__((aligned(16)));;
  241. #ifdef CONFIG_X86_32
  242. /* virtual 86 mode info */
  243. struct vm86_struct __user *vm86_info;
  244. unsigned long screen_bitmap;
  245. unsigned long v86flags, v86mask, saved_sp0;
  246. unsigned int saved_fs, saved_gs;
  247. #endif
  248. /* IO permissions */
  249. unsigned long *io_bitmap_ptr;
  250. unsigned long iopl;
  251. /* max allowed port in the bitmap, in bytes: */
  252. unsigned io_bitmap_max;
  253. /* MSR_IA32_DEBUGCTLMSR value to switch in if TIF_DEBUGCTLMSR is set. */
  254. unsigned long debugctlmsr;
  255. /* Debug Store - if not 0 points to a DS Save Area configuration;
  256. * goes into MSR_IA32_DS_AREA */
  257. unsigned long ds_area_msr;
  258. };
  259. static inline unsigned long native_get_debugreg(int regno)
  260. {
  261. unsigned long val = 0; /* Damn you, gcc! */
  262. switch (regno) {
  263. case 0:
  264. asm("mov %%db0, %0" :"=r" (val)); break;
  265. case 1:
  266. asm("mov %%db1, %0" :"=r" (val)); break;
  267. case 2:
  268. asm("mov %%db2, %0" :"=r" (val)); break;
  269. case 3:
  270. asm("mov %%db3, %0" :"=r" (val)); break;
  271. case 6:
  272. asm("mov %%db6, %0" :"=r" (val)); break;
  273. case 7:
  274. asm("mov %%db7, %0" :"=r" (val)); break;
  275. default:
  276. BUG();
  277. }
  278. return val;
  279. }
  280. static inline void native_set_debugreg(int regno, unsigned long value)
  281. {
  282. switch (regno) {
  283. case 0:
  284. asm("mov %0,%%db0" : /* no output */ :"r" (value));
  285. break;
  286. case 1:
  287. asm("mov %0,%%db1" : /* no output */ :"r" (value));
  288. break;
  289. case 2:
  290. asm("mov %0,%%db2" : /* no output */ :"r" (value));
  291. break;
  292. case 3:
  293. asm("mov %0,%%db3" : /* no output */ :"r" (value));
  294. break;
  295. case 6:
  296. asm("mov %0,%%db6" : /* no output */ :"r" (value));
  297. break;
  298. case 7:
  299. asm("mov %0,%%db7" : /* no output */ :"r" (value));
  300. break;
  301. default:
  302. BUG();
  303. }
  304. }
  305. /*
  306. * Set IOPL bits in EFLAGS from given mask
  307. */
  308. static inline void native_set_iopl_mask(unsigned mask)
  309. {
  310. #ifdef CONFIG_X86_32
  311. unsigned int reg;
  312. __asm__ __volatile__ ("pushfl;"
  313. "popl %0;"
  314. "andl %1, %0;"
  315. "orl %2, %0;"
  316. "pushl %0;"
  317. "popfl"
  318. : "=&r" (reg)
  319. : "i" (~X86_EFLAGS_IOPL), "r" (mask));
  320. #endif
  321. }
  322. static inline void native_load_sp0(struct tss_struct *tss,
  323. struct thread_struct *thread)
  324. {
  325. tss->x86_tss.sp0 = thread->sp0;
  326. #ifdef CONFIG_X86_32
  327. /* Only happens when SEP is enabled, no need to test "SEP"arately */
  328. if (unlikely(tss->x86_tss.ss1 != thread->sysenter_cs)) {
  329. tss->x86_tss.ss1 = thread->sysenter_cs;
  330. wrmsr(MSR_IA32_SYSENTER_CS, thread->sysenter_cs, 0);
  331. }
  332. #endif
  333. }
  334. #ifdef CONFIG_PARAVIRT
  335. #include <asm/paravirt.h>
  336. #else
  337. #define __cpuid native_cpuid
  338. #define paravirt_enabled() 0
  339. /*
  340. * These special macros can be used to get or set a debugging register
  341. */
  342. #define get_debugreg(var, register) \
  343. (var) = native_get_debugreg(register)
  344. #define set_debugreg(value, register) \
  345. native_set_debugreg(register, value)
  346. static inline void load_sp0(struct tss_struct *tss,
  347. struct thread_struct *thread)
  348. {
  349. native_load_sp0(tss, thread);
  350. }
  351. #define set_iopl_mask native_set_iopl_mask
  352. #endif /* CONFIG_PARAVIRT */
  353. /*
  354. * Save the cr4 feature set we're using (ie
  355. * Pentium 4MB enable and PPro Global page
  356. * enable), so that any CPU's that boot up
  357. * after us can get the correct flags.
  358. */
  359. extern unsigned long mmu_cr4_features;
  360. static inline void set_in_cr4(unsigned long mask)
  361. {
  362. unsigned cr4;
  363. mmu_cr4_features |= mask;
  364. cr4 = read_cr4();
  365. cr4 |= mask;
  366. write_cr4(cr4);
  367. }
  368. static inline void clear_in_cr4(unsigned long mask)
  369. {
  370. unsigned cr4;
  371. mmu_cr4_features &= ~mask;
  372. cr4 = read_cr4();
  373. cr4 &= ~mask;
  374. write_cr4(cr4);
  375. }
  376. struct microcode_header {
  377. unsigned int hdrver;
  378. unsigned int rev;
  379. unsigned int date;
  380. unsigned int sig;
  381. unsigned int cksum;
  382. unsigned int ldrver;
  383. unsigned int pf;
  384. unsigned int datasize;
  385. unsigned int totalsize;
  386. unsigned int reserved[3];
  387. };
  388. struct microcode {
  389. struct microcode_header hdr;
  390. unsigned int bits[0];
  391. };
  392. typedef struct microcode microcode_t;
  393. typedef struct microcode_header microcode_header_t;
  394. /* microcode format is extended from prescott processors */
  395. struct extended_signature {
  396. unsigned int sig;
  397. unsigned int pf;
  398. unsigned int cksum;
  399. };
  400. struct extended_sigtable {
  401. unsigned int count;
  402. unsigned int cksum;
  403. unsigned int reserved[3];
  404. struct extended_signature sigs[0];
  405. };
  406. typedef struct {
  407. unsigned long seg;
  408. } mm_segment_t;
  409. /*
  410. * create a kernel thread without removing it from tasklists
  411. */
  412. extern int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags);
  413. /* Free all resources held by a thread. */
  414. extern void release_thread(struct task_struct *);
  415. /* Prepare to copy thread state - unlazy all lazy status */
  416. extern void prepare_to_copy(struct task_struct *tsk);
  417. unsigned long get_wchan(struct task_struct *p);
  418. /*
  419. * Generic CPUID function
  420. * clear %ecx since some cpus (Cyrix MII) do not set or clear %ecx
  421. * resulting in stale register contents being returned.
  422. */
  423. static inline void cpuid(unsigned int op,
  424. unsigned int *eax, unsigned int *ebx,
  425. unsigned int *ecx, unsigned int *edx)
  426. {
  427. *eax = op;
  428. *ecx = 0;
  429. __cpuid(eax, ebx, ecx, edx);
  430. }
  431. /* Some CPUID calls want 'count' to be placed in ecx */
  432. static inline void cpuid_count(unsigned int op, int count,
  433. unsigned int *eax, unsigned int *ebx,
  434. unsigned int *ecx, unsigned int *edx)
  435. {
  436. *eax = op;
  437. *ecx = count;
  438. __cpuid(eax, ebx, ecx, edx);
  439. }
  440. /*
  441. * CPUID functions returning a single datum
  442. */
  443. static inline unsigned int cpuid_eax(unsigned int op)
  444. {
  445. unsigned int eax, ebx, ecx, edx;
  446. cpuid(op, &eax, &ebx, &ecx, &edx);
  447. return eax;
  448. }
  449. static inline unsigned int cpuid_ebx(unsigned int op)
  450. {
  451. unsigned int eax, ebx, ecx, edx;
  452. cpuid(op, &eax, &ebx, &ecx, &edx);
  453. return ebx;
  454. }
  455. static inline unsigned int cpuid_ecx(unsigned int op)
  456. {
  457. unsigned int eax, ebx, ecx, edx;
  458. cpuid(op, &eax, &ebx, &ecx, &edx);
  459. return ecx;
  460. }
  461. static inline unsigned int cpuid_edx(unsigned int op)
  462. {
  463. unsigned int eax, ebx, ecx, edx;
  464. cpuid(op, &eax, &ebx, &ecx, &edx);
  465. return edx;
  466. }
  467. /* REP NOP (PAUSE) is a good thing to insert into busy-wait loops. */
  468. static inline void rep_nop(void)
  469. {
  470. __asm__ __volatile__("rep;nop": : :"memory");
  471. }
  472. /* Stop speculative execution */
  473. static inline void sync_core(void)
  474. {
  475. int tmp;
  476. asm volatile("cpuid" : "=a" (tmp) : "0" (1)
  477. : "ebx", "ecx", "edx", "memory");
  478. }
  479. #define cpu_relax() rep_nop()
  480. static inline void __monitor(const void *eax, unsigned long ecx,
  481. unsigned long edx)
  482. {
  483. /* "monitor %eax,%ecx,%edx;" */
  484. asm volatile(
  485. ".byte 0x0f,0x01,0xc8;"
  486. : :"a" (eax), "c" (ecx), "d"(edx));
  487. }
  488. static inline void __mwait(unsigned long eax, unsigned long ecx)
  489. {
  490. /* "mwait %eax,%ecx;" */
  491. asm volatile(
  492. ".byte 0x0f,0x01,0xc9;"
  493. : :"a" (eax), "c" (ecx));
  494. }
  495. static inline void __sti_mwait(unsigned long eax, unsigned long ecx)
  496. {
  497. /* "mwait %eax,%ecx;" */
  498. asm volatile(
  499. "sti; .byte 0x0f,0x01,0xc9;"
  500. : :"a" (eax), "c" (ecx));
  501. }
  502. extern void mwait_idle_with_hints(unsigned long eax, unsigned long ecx);
  503. extern int force_mwait;
  504. extern void select_idle_routine(const struct cpuinfo_x86 *c);
  505. extern unsigned long boot_option_idle_override;
  506. extern void enable_sep_cpu(void);
  507. extern int sysenter_setup(void);
  508. /* Defined in head.S */
  509. extern struct desc_ptr early_gdt_descr;
  510. extern void cpu_set_gdt(int);
  511. extern void switch_to_new_gdt(void);
  512. extern void cpu_init(void);
  513. extern void init_gdt(int cpu);
  514. /* from system description table in BIOS. Mostly for MCA use, but
  515. * others may find it useful. */
  516. extern unsigned int machine_id;
  517. extern unsigned int machine_submodel_id;
  518. extern unsigned int BIOS_revision;
  519. extern unsigned int mca_pentium_flag;
  520. /* Boot loader type from the setup header */
  521. extern int bootloader_type;
  522. extern char ignore_fpu_irq;
  523. #define cache_line_size() (boot_cpu_data.x86_cache_alignment)
  524. #define HAVE_ARCH_PICK_MMAP_LAYOUT 1
  525. #define ARCH_HAS_PREFETCHW
  526. #define ARCH_HAS_SPINLOCK_PREFETCH
  527. #ifdef CONFIG_X86_32
  528. #define BASE_PREFETCH ASM_NOP4
  529. #define ARCH_HAS_PREFETCH
  530. #else
  531. #define BASE_PREFETCH "prefetcht0 (%1)"
  532. #endif
  533. /* Prefetch instructions for Pentium III and AMD Athlon */
  534. /* It's not worth to care about 3dnow! prefetches for the K6
  535. because they are microcoded there and very slow.
  536. However we don't do prefetches for pre XP Athlons currently
  537. That should be fixed. */
  538. static inline void prefetch(const void *x)
  539. {
  540. alternative_input(BASE_PREFETCH,
  541. "prefetchnta (%1)",
  542. X86_FEATURE_XMM,
  543. "r" (x));
  544. }
  545. /* 3dnow! prefetch to get an exclusive cache line. Useful for
  546. spinlocks to avoid one state transition in the cache coherency protocol. */
  547. static inline void prefetchw(const void *x)
  548. {
  549. alternative_input(BASE_PREFETCH,
  550. "prefetchw (%1)",
  551. X86_FEATURE_3DNOW,
  552. "r" (x));
  553. }
  554. #define spin_lock_prefetch(x) prefetchw(x)
  555. /* This decides where the kernel will search for a free chunk of vm
  556. * space during mmap's.
  557. */
  558. #define TASK_UNMAPPED_BASE (PAGE_ALIGN(TASK_SIZE / 3))
  559. #define KSTK_EIP(task) (task_pt_regs(task)->ip)
  560. #endif