processor.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * include/asm-x86_64/processor.h
  3. *
  4. * Copyright (C) 1994 Linus Torvalds
  5. */
  6. #ifndef __ASM_X86_64_PROCESSOR_H
  7. #define __ASM_X86_64_PROCESSOR_H
  8. #include <asm/segment.h>
  9. #include <asm/page.h>
  10. #include <asm/types.h>
  11. #include <asm/sigcontext.h>
  12. #include <asm/cpufeature.h>
  13. #include <linux/threads.h>
  14. #include <asm/msr.h>
  15. #include <asm/current.h>
  16. #include <asm/system.h>
  17. #include <asm/mmsegment.h>
  18. #include <asm/percpu.h>
  19. #include <linux/personality.h>
  20. #include <linux/cpumask.h>
  21. #define TF_MASK 0x00000100
  22. #define IF_MASK 0x00000200
  23. #define IOPL_MASK 0x00003000
  24. #define NT_MASK 0x00004000
  25. #define VM_MASK 0x00020000
  26. #define AC_MASK 0x00040000
  27. #define VIF_MASK 0x00080000 /* virtual interrupt flag */
  28. #define VIP_MASK 0x00100000 /* virtual interrupt pending */
  29. #define ID_MASK 0x00200000
  30. #define desc_empty(desc) \
  31. (!((desc)->a | (desc)->b))
  32. #define desc_equal(desc1, desc2) \
  33. (((desc1)->a == (desc2)->a) && ((desc1)->b == (desc2)->b))
  34. /*
  35. * Default implementation of macro that returns current
  36. * instruction pointer ("program counter").
  37. */
  38. #define current_text_addr() ({ void *pc; asm volatile("leaq 1f(%%rip),%0\n1:":"=r"(pc)); pc; })
  39. /*
  40. * CPU type and hardware bug flags. Kept separately for each CPU.
  41. */
  42. struct cpuinfo_x86 {
  43. __u8 x86; /* CPU family */
  44. __u8 x86_vendor; /* CPU vendor */
  45. __u8 x86_model;
  46. __u8 x86_mask;
  47. int cpuid_level; /* Maximum supported CPUID level, -1=no CPUID */
  48. __u32 x86_capability[NCAPINTS];
  49. char x86_vendor_id[16];
  50. char x86_model_id[64];
  51. int x86_cache_size; /* in KB */
  52. int x86_clflush_size;
  53. int x86_cache_alignment;
  54. int x86_tlbsize; /* number of 4K pages in DTLB/ITLB combined(in pages)*/
  55. __u8 x86_virt_bits, x86_phys_bits;
  56. __u8 x86_max_cores; /* cpuid returned max cores value */
  57. __u32 x86_power;
  58. __u32 extended_cpuid_level; /* Max extended CPUID function supported */
  59. unsigned long loops_per_jiffy;
  60. #ifdef CONFIG_SMP
  61. cpumask_t llc_shared_map; /* cpus sharing the last level cache */
  62. #endif
  63. __u8 apicid;
  64. #ifdef CONFIG_SMP
  65. __u8 booted_cores; /* number of cores as seen by OS */
  66. __u8 phys_proc_id; /* Physical Processor id. */
  67. __u8 cpu_core_id; /* Core id. */
  68. #endif
  69. } ____cacheline_aligned;
  70. #define X86_VENDOR_INTEL 0
  71. #define X86_VENDOR_CYRIX 1
  72. #define X86_VENDOR_AMD 2
  73. #define X86_VENDOR_UMC 3
  74. #define X86_VENDOR_NEXGEN 4
  75. #define X86_VENDOR_CENTAUR 5
  76. #define X86_VENDOR_RISE 6
  77. #define X86_VENDOR_TRANSMETA 7
  78. #define X86_VENDOR_NUM 8
  79. #define X86_VENDOR_UNKNOWN 0xff
  80. #ifdef CONFIG_SMP
  81. extern struct cpuinfo_x86 cpu_data[];
  82. #define current_cpu_data cpu_data[smp_processor_id()]
  83. #else
  84. #define cpu_data (&boot_cpu_data)
  85. #define current_cpu_data boot_cpu_data
  86. #endif
  87. extern char ignore_irq13;
  88. extern void identify_cpu(struct cpuinfo_x86 *);
  89. extern void print_cpu_info(struct cpuinfo_x86 *);
  90. extern unsigned int init_intel_cacheinfo(struct cpuinfo_x86 *c);
  91. extern unsigned short num_cache_leaves;
  92. /*
  93. * EFLAGS bits
  94. */
  95. #define X86_EFLAGS_CF 0x00000001 /* Carry Flag */
  96. #define X86_EFLAGS_PF 0x00000004 /* Parity Flag */
  97. #define X86_EFLAGS_AF 0x00000010 /* Auxillary carry Flag */
  98. #define X86_EFLAGS_ZF 0x00000040 /* Zero Flag */
  99. #define X86_EFLAGS_SF 0x00000080 /* Sign Flag */
  100. #define X86_EFLAGS_TF 0x00000100 /* Trap Flag */
  101. #define X86_EFLAGS_IF 0x00000200 /* Interrupt Flag */
  102. #define X86_EFLAGS_DF 0x00000400 /* Direction Flag */
  103. #define X86_EFLAGS_OF 0x00000800 /* Overflow Flag */
  104. #define X86_EFLAGS_IOPL 0x00003000 /* IOPL mask */
  105. #define X86_EFLAGS_NT 0x00004000 /* Nested Task */
  106. #define X86_EFLAGS_RF 0x00010000 /* Resume Flag */
  107. #define X86_EFLAGS_VM 0x00020000 /* Virtual Mode */
  108. #define X86_EFLAGS_AC 0x00040000 /* Alignment Check */
  109. #define X86_EFLAGS_VIF 0x00080000 /* Virtual Interrupt Flag */
  110. #define X86_EFLAGS_VIP 0x00100000 /* Virtual Interrupt Pending */
  111. #define X86_EFLAGS_ID 0x00200000 /* CPUID detection flag */
  112. /*
  113. * Intel CPU features in CR4
  114. */
  115. #define X86_CR4_VME 0x0001 /* enable vm86 extensions */
  116. #define X86_CR4_PVI 0x0002 /* virtual interrupts flag enable */
  117. #define X86_CR4_TSD 0x0004 /* disable time stamp at ipl 3 */
  118. #define X86_CR4_DE 0x0008 /* enable debugging extensions */
  119. #define X86_CR4_PSE 0x0010 /* enable page size extensions */
  120. #define X86_CR4_PAE 0x0020 /* enable physical address extensions */
  121. #define X86_CR4_MCE 0x0040 /* Machine check enable */
  122. #define X86_CR4_PGE 0x0080 /* enable global pages */
  123. #define X86_CR4_PCE 0x0100 /* enable performance counters at ipl 3 */
  124. #define X86_CR4_OSFXSR 0x0200 /* enable fast FPU save and restore */
  125. #define X86_CR4_OSXMMEXCPT 0x0400 /* enable unmasked SSE exceptions */
  126. /*
  127. * Save the cr4 feature set we're using (ie
  128. * Pentium 4MB enable and PPro Global page
  129. * enable), so that any CPU's that boot up
  130. * after us can get the correct flags.
  131. */
  132. extern unsigned long mmu_cr4_features;
  133. static inline void set_in_cr4 (unsigned long mask)
  134. {
  135. mmu_cr4_features |= mask;
  136. __asm__("movq %%cr4,%%rax\n\t"
  137. "orq %0,%%rax\n\t"
  138. "movq %%rax,%%cr4\n"
  139. : : "irg" (mask)
  140. :"ax");
  141. }
  142. static inline void clear_in_cr4 (unsigned long mask)
  143. {
  144. mmu_cr4_features &= ~mask;
  145. __asm__("movq %%cr4,%%rax\n\t"
  146. "andq %0,%%rax\n\t"
  147. "movq %%rax,%%cr4\n"
  148. : : "irg" (~mask)
  149. :"ax");
  150. }
  151. /*
  152. * User space process size. 47bits minus one guard page.
  153. */
  154. #define TASK_SIZE64 (0x800000000000UL - 4096)
  155. /* This decides where the kernel will search for a free chunk of vm
  156. * space during mmap's.
  157. */
  158. #define IA32_PAGE_OFFSET ((current->personality & ADDR_LIMIT_3GB) ? 0xc0000000 : 0xFFFFe000)
  159. #define TASK_SIZE (test_thread_flag(TIF_IA32) ? IA32_PAGE_OFFSET : TASK_SIZE64)
  160. #define TASK_SIZE_OF(child) ((test_tsk_thread_flag(child, TIF_IA32)) ? IA32_PAGE_OFFSET : TASK_SIZE64)
  161. #define TASK_UNMAPPED_BASE PAGE_ALIGN(TASK_SIZE/3)
  162. /*
  163. * Size of io_bitmap.
  164. */
  165. #define IO_BITMAP_BITS 65536
  166. #define IO_BITMAP_BYTES (IO_BITMAP_BITS/8)
  167. #define IO_BITMAP_LONGS (IO_BITMAP_BYTES/sizeof(long))
  168. #define IO_BITMAP_OFFSET offsetof(struct tss_struct,io_bitmap)
  169. #define INVALID_IO_BITMAP_OFFSET 0x8000
  170. struct i387_fxsave_struct {
  171. u16 cwd;
  172. u16 swd;
  173. u16 twd;
  174. u16 fop;
  175. u64 rip;
  176. u64 rdp;
  177. u32 mxcsr;
  178. u32 mxcsr_mask;
  179. u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
  180. u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 128 bytes */
  181. u32 padding[24];
  182. } __attribute__ ((aligned (16)));
  183. union i387_union {
  184. struct i387_fxsave_struct fxsave;
  185. };
  186. struct tss_struct {
  187. u32 reserved1;
  188. u64 rsp0;
  189. u64 rsp1;
  190. u64 rsp2;
  191. u64 reserved2;
  192. u64 ist[7];
  193. u32 reserved3;
  194. u32 reserved4;
  195. u16 reserved5;
  196. u16 io_bitmap_base;
  197. /*
  198. * The extra 1 is there because the CPU will access an
  199. * additional byte beyond the end of the IO permission
  200. * bitmap. The extra byte must be all 1 bits, and must
  201. * be within the limit. Thus we have:
  202. *
  203. * 128 bytes, the bitmap itself, for ports 0..0x3ff
  204. * 8 bytes, for an extra "long" of ~0UL
  205. */
  206. unsigned long io_bitmap[IO_BITMAP_LONGS + 1];
  207. } __attribute__((packed)) ____cacheline_aligned;
  208. extern struct cpuinfo_x86 boot_cpu_data;
  209. DECLARE_PER_CPU(struct tss_struct,init_tss);
  210. /* Save the original ist values for checking stack pointers during debugging */
  211. struct orig_ist {
  212. unsigned long ist[7];
  213. };
  214. DECLARE_PER_CPU(struct orig_ist, orig_ist);
  215. #ifdef CONFIG_X86_VSMP
  216. #define ARCH_MIN_TASKALIGN (1 << INTERNODE_CACHE_SHIFT)
  217. #define ARCH_MIN_MMSTRUCT_ALIGN (1 << INTERNODE_CACHE_SHIFT)
  218. #else
  219. #define ARCH_MIN_TASKALIGN 16
  220. #define ARCH_MIN_MMSTRUCT_ALIGN 0
  221. #endif
  222. struct thread_struct {
  223. unsigned long rsp0;
  224. unsigned long rsp;
  225. unsigned long userrsp; /* Copy from PDA */
  226. unsigned long fs;
  227. unsigned long gs;
  228. unsigned short es, ds, fsindex, gsindex;
  229. /* Hardware debugging registers */
  230. unsigned long debugreg0;
  231. unsigned long debugreg1;
  232. unsigned long debugreg2;
  233. unsigned long debugreg3;
  234. unsigned long debugreg6;
  235. unsigned long debugreg7;
  236. /* fault info */
  237. unsigned long cr2, trap_no, error_code;
  238. /* floating point info */
  239. union i387_union i387 __attribute__((aligned(16)));
  240. /* IO permissions. the bitmap could be moved into the GDT, that would make
  241. switch faster for a limited number of ioperm using tasks. -AK */
  242. int ioperm;
  243. unsigned long *io_bitmap_ptr;
  244. unsigned io_bitmap_max;
  245. /* cached TLS descriptors. */
  246. u64 tls_array[GDT_ENTRY_TLS_ENTRIES];
  247. } __attribute__((aligned(16)));
  248. #define INIT_THREAD { \
  249. .rsp0 = (unsigned long)&init_stack + sizeof(init_stack) \
  250. }
  251. #define INIT_TSS { \
  252. .rsp0 = (unsigned long)&init_stack + sizeof(init_stack) \
  253. }
  254. #define INIT_MMAP \
  255. { &init_mm, 0, 0, NULL, PAGE_SHARED, VM_READ | VM_WRITE | VM_EXEC, 1, NULL, NULL }
  256. #define start_thread(regs,new_rip,new_rsp) do { \
  257. asm volatile("movl %0,%%fs; movl %0,%%es; movl %0,%%ds": :"r" (0)); \
  258. load_gs_index(0); \
  259. (regs)->rip = (new_rip); \
  260. (regs)->rsp = (new_rsp); \
  261. write_pda(oldrsp, (new_rsp)); \
  262. (regs)->cs = __USER_CS; \
  263. (regs)->ss = __USER_DS; \
  264. (regs)->eflags = 0x200; \
  265. set_fs(USER_DS); \
  266. } while(0)
  267. #define get_debugreg(var, register) \
  268. __asm__("movq %%db" #register ", %0" \
  269. :"=r" (var))
  270. #define set_debugreg(value, register) \
  271. __asm__("movq %0,%%db" #register \
  272. : /* no output */ \
  273. :"r" (value))
  274. struct task_struct;
  275. struct mm_struct;
  276. /* Free all resources held by a thread. */
  277. extern void release_thread(struct task_struct *);
  278. /* Prepare to copy thread state - unlazy all lazy status */
  279. extern void prepare_to_copy(struct task_struct *tsk);
  280. /*
  281. * create a kernel thread without removing it from tasklists
  282. */
  283. extern long kernel_thread(int (*fn)(void *), void * arg, unsigned long flags);
  284. /*
  285. * Return saved PC of a blocked thread.
  286. * What is this good for? it will be always the scheduler or ret_from_fork.
  287. */
  288. #define thread_saved_pc(t) (*(unsigned long *)((t)->thread.rsp - 8))
  289. extern unsigned long get_wchan(struct task_struct *p);
  290. #define task_pt_regs(tsk) ((struct pt_regs *)(tsk)->thread.rsp0 - 1)
  291. #define KSTK_EIP(tsk) (task_pt_regs(tsk)->rip)
  292. #define KSTK_ESP(tsk) -1 /* sorry. doesn't work for syscall. */
  293. struct microcode_header {
  294. unsigned int hdrver;
  295. unsigned int rev;
  296. unsigned int date;
  297. unsigned int sig;
  298. unsigned int cksum;
  299. unsigned int ldrver;
  300. unsigned int pf;
  301. unsigned int datasize;
  302. unsigned int totalsize;
  303. unsigned int reserved[3];
  304. };
  305. struct microcode {
  306. struct microcode_header hdr;
  307. unsigned int bits[0];
  308. };
  309. typedef struct microcode microcode_t;
  310. typedef struct microcode_header microcode_header_t;
  311. /* microcode format is extended from prescott processors */
  312. struct extended_signature {
  313. unsigned int sig;
  314. unsigned int pf;
  315. unsigned int cksum;
  316. };
  317. struct extended_sigtable {
  318. unsigned int count;
  319. unsigned int cksum;
  320. unsigned int reserved[3];
  321. struct extended_signature sigs[0];
  322. };
  323. #define ASM_NOP1 K8_NOP1
  324. #define ASM_NOP2 K8_NOP2
  325. #define ASM_NOP3 K8_NOP3
  326. #define ASM_NOP4 K8_NOP4
  327. #define ASM_NOP5 K8_NOP5
  328. #define ASM_NOP6 K8_NOP6
  329. #define ASM_NOP7 K8_NOP7
  330. #define ASM_NOP8 K8_NOP8
  331. /* Opteron nops */
  332. #define K8_NOP1 ".byte 0x90\n"
  333. #define K8_NOP2 ".byte 0x66,0x90\n"
  334. #define K8_NOP3 ".byte 0x66,0x66,0x90\n"
  335. #define K8_NOP4 ".byte 0x66,0x66,0x66,0x90\n"
  336. #define K8_NOP5 K8_NOP3 K8_NOP2
  337. #define K8_NOP6 K8_NOP3 K8_NOP3
  338. #define K8_NOP7 K8_NOP4 K8_NOP3
  339. #define K8_NOP8 K8_NOP4 K8_NOP4
  340. #define ASM_NOP_MAX 8
  341. /* REP NOP (PAUSE) is a good thing to insert into busy-wait loops. */
  342. static inline void rep_nop(void)
  343. {
  344. __asm__ __volatile__("rep;nop": : :"memory");
  345. }
  346. /* Stop speculative execution */
  347. static inline void sync_core(void)
  348. {
  349. int tmp;
  350. asm volatile("cpuid" : "=a" (tmp) : "0" (1) : "ebx","ecx","edx","memory");
  351. }
  352. #define cpu_has_fpu 1
  353. #define ARCH_HAS_PREFETCH
  354. static inline void prefetch(void *x)
  355. {
  356. asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
  357. }
  358. #define ARCH_HAS_PREFETCHW 1
  359. static inline void prefetchw(void *x)
  360. {
  361. alternative_input("prefetcht0 (%1)",
  362. "prefetchw (%1)",
  363. X86_FEATURE_3DNOW,
  364. "r" (x));
  365. }
  366. #define ARCH_HAS_SPINLOCK_PREFETCH 1
  367. #define spin_lock_prefetch(x) prefetchw(x)
  368. #define cpu_relax() rep_nop()
  369. /*
  370. * NSC/Cyrix CPU configuration register indexes
  371. */
  372. #define CX86_CCR0 0xc0
  373. #define CX86_CCR1 0xc1
  374. #define CX86_CCR2 0xc2
  375. #define CX86_CCR3 0xc3
  376. #define CX86_CCR4 0xe8
  377. #define CX86_CCR5 0xe9
  378. #define CX86_CCR6 0xea
  379. #define CX86_CCR7 0xeb
  380. #define CX86_DIR0 0xfe
  381. #define CX86_DIR1 0xff
  382. #define CX86_ARR_BASE 0xc4
  383. #define CX86_RCR_BASE 0xdc
  384. /*
  385. * NSC/Cyrix CPU indexed register access macros
  386. */
  387. #define getCx86(reg) ({ outb((reg), 0x22); inb(0x23); })
  388. #define setCx86(reg, data) do { \
  389. outb((reg), 0x22); \
  390. outb((data), 0x23); \
  391. } while (0)
  392. static inline void serialize_cpu(void)
  393. {
  394. __asm__ __volatile__ ("cpuid" : : : "ax", "bx", "cx", "dx");
  395. }
  396. static inline void __monitor(const void *eax, unsigned long ecx,
  397. unsigned long edx)
  398. {
  399. /* "monitor %eax,%ecx,%edx;" */
  400. asm volatile(
  401. ".byte 0x0f,0x01,0xc8;"
  402. : :"a" (eax), "c" (ecx), "d"(edx));
  403. }
  404. static inline void __mwait(unsigned long eax, unsigned long ecx)
  405. {
  406. /* "mwait %eax,%ecx;" */
  407. asm volatile(
  408. ".byte 0x0f,0x01,0xc9;"
  409. : :"a" (eax), "c" (ecx));
  410. }
  411. extern void mwait_idle_with_hints(unsigned long eax, unsigned long ecx);
  412. #define stack_current() \
  413. ({ \
  414. struct thread_info *ti; \
  415. asm("andq %%rsp,%0; ":"=r" (ti) : "0" (CURRENT_MASK)); \
  416. ti->task; \
  417. })
  418. #define cache_line_size() (boot_cpu_data.x86_cache_alignment)
  419. extern unsigned long boot_option_idle_override;
  420. /* Boot loader type from the setup header */
  421. extern int bootloader_type;
  422. #define HAVE_ARCH_PICK_MMAP_LAYOUT 1
  423. #endif /* __ASM_X86_64_PROCESSOR_H */