processor.h 13 KB

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