processor_64.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * Copyright (C) 1994 Linus Torvalds
  3. */
  4. #ifndef __ASM_X86_64_PROCESSOR_H
  5. #define __ASM_X86_64_PROCESSOR_H
  6. #include <asm/segment.h>
  7. #include <asm/page.h>
  8. #include <asm/types.h>
  9. #include <asm/sigcontext.h>
  10. #include <asm/cpufeature.h>
  11. #include <linux/threads.h>
  12. #include <asm/msr.h>
  13. #include <asm/current.h>
  14. #include <asm/system.h>
  15. #include <asm/mmsegment.h>
  16. #include <asm/percpu.h>
  17. #include <linux/personality.h>
  18. #include <linux/cpumask.h>
  19. #include <asm/processor-flags.h>
  20. #define TF_MASK 0x00000100
  21. #define IF_MASK 0x00000200
  22. #define IOPL_MASK 0x00003000
  23. #define NT_MASK 0x00004000
  24. #define VM_MASK 0x00020000
  25. #define AC_MASK 0x00040000
  26. #define VIF_MASK 0x00080000 /* virtual interrupt flag */
  27. #define VIP_MASK 0x00100000 /* virtual interrupt pending */
  28. #define ID_MASK 0x00200000
  29. static inline int desc_empty(const void *ptr)
  30. {
  31. const u32 *desc = ptr;
  32. return !(desc[0] | desc[1]);
  33. }
  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. __u8 x86_coreid_bits; /* cpuid returned core id bits */
  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. #ifdef CONFIG_SMP
  66. __u8 booted_cores; /* number of cores as seen by OS */
  67. __u8 phys_proc_id; /* Physical Processor id. */
  68. __u8 cpu_core_id; /* Core id. */
  69. __u8 cpu_index; /* index into per_cpu list */
  70. #endif
  71. } ____cacheline_aligned;
  72. #define X86_VENDOR_INTEL 0
  73. #define X86_VENDOR_CYRIX 1
  74. #define X86_VENDOR_AMD 2
  75. #define X86_VENDOR_UMC 3
  76. #define X86_VENDOR_NEXGEN 4
  77. #define X86_VENDOR_CENTAUR 5
  78. #define X86_VENDOR_TRANSMETA 7
  79. #define X86_VENDOR_NUM 8
  80. #define X86_VENDOR_UNKNOWN 0xff
  81. #ifdef CONFIG_SMP
  82. DECLARE_PER_CPU(struct cpuinfo_x86, cpu_info);
  83. #define cpu_data(cpu) per_cpu(cpu_info, cpu)
  84. #define current_cpu_data cpu_data(smp_processor_id())
  85. #else
  86. #define cpu_data(cpu) boot_cpu_data
  87. #define current_cpu_data boot_cpu_data
  88. #endif
  89. extern char ignore_irq13;
  90. extern void identify_cpu(struct cpuinfo_x86 *);
  91. extern void print_cpu_info(struct cpuinfo_x86 *);
  92. extern void init_scattered_cpuid_features(struct cpuinfo_x86 *c);
  93. extern unsigned int init_intel_cacheinfo(struct cpuinfo_x86 *c);
  94. extern unsigned short num_cache_leaves;
  95. /*
  96. * Save the cr4 feature set we're using (ie
  97. * Pentium 4MB enable and PPro Global page
  98. * enable), so that any CPU's that boot up
  99. * after us can get the correct flags.
  100. */
  101. extern unsigned long mmu_cr4_features;
  102. static inline void set_in_cr4 (unsigned long mask)
  103. {
  104. mmu_cr4_features |= mask;
  105. __asm__("movq %%cr4,%%rax\n\t"
  106. "orq %0,%%rax\n\t"
  107. "movq %%rax,%%cr4\n"
  108. : : "irg" (mask)
  109. :"ax");
  110. }
  111. static inline void clear_in_cr4 (unsigned long mask)
  112. {
  113. mmu_cr4_features &= ~mask;
  114. __asm__("movq %%cr4,%%rax\n\t"
  115. "andq %0,%%rax\n\t"
  116. "movq %%rax,%%cr4\n"
  117. : : "irg" (~mask)
  118. :"ax");
  119. }
  120. /*
  121. * User space process size. 47bits minus one guard page.
  122. */
  123. #define TASK_SIZE64 (0x800000000000UL - 4096)
  124. /* This decides where the kernel will search for a free chunk of vm
  125. * space during mmap's.
  126. */
  127. #define IA32_PAGE_OFFSET ((current->personality & ADDR_LIMIT_3GB) ? 0xc0000000 : 0xFFFFe000)
  128. #define TASK_SIZE (test_thread_flag(TIF_IA32) ? IA32_PAGE_OFFSET : TASK_SIZE64)
  129. #define TASK_SIZE_OF(child) ((test_tsk_thread_flag(child, TIF_IA32)) ? IA32_PAGE_OFFSET : TASK_SIZE64)
  130. #define TASK_UNMAPPED_BASE PAGE_ALIGN(TASK_SIZE/3)
  131. /*
  132. * Size of io_bitmap.
  133. */
  134. #define IO_BITMAP_BITS 65536
  135. #define IO_BITMAP_BYTES (IO_BITMAP_BITS/8)
  136. #define IO_BITMAP_LONGS (IO_BITMAP_BYTES/sizeof(long))
  137. #define IO_BITMAP_OFFSET offsetof(struct tss_struct,io_bitmap)
  138. #define INVALID_IO_BITMAP_OFFSET 0x8000
  139. struct i387_fxsave_struct {
  140. u16 cwd;
  141. u16 swd;
  142. u16 twd;
  143. u16 fop;
  144. u64 rip;
  145. u64 rdp;
  146. u32 mxcsr;
  147. u32 mxcsr_mask;
  148. u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
  149. u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
  150. u32 padding[24];
  151. } __attribute__ ((aligned (16)));
  152. union i387_union {
  153. struct i387_fxsave_struct fxsave;
  154. };
  155. struct tss_struct {
  156. u32 reserved1;
  157. u64 sp0;
  158. u64 sp1;
  159. u64 sp2;
  160. u64 reserved2;
  161. u64 ist[7];
  162. u32 reserved3;
  163. u32 reserved4;
  164. u16 reserved5;
  165. u16 io_bitmap_base;
  166. /*
  167. * The extra 1 is there because the CPU will access an
  168. * additional byte beyond the end of the IO permission
  169. * bitmap. The extra byte must be all 1 bits, and must
  170. * be within the limit. Thus we have:
  171. *
  172. * 128 bytes, the bitmap itself, for ports 0..0x3ff
  173. * 8 bytes, for an extra "long" of ~0UL
  174. */
  175. unsigned long io_bitmap[IO_BITMAP_LONGS + 1];
  176. } __attribute__((packed)) ____cacheline_aligned;
  177. extern struct cpuinfo_x86 boot_cpu_data;
  178. DECLARE_PER_CPU(struct tss_struct,init_tss);
  179. /* Save the original ist values for checking stack pointers during debugging */
  180. struct orig_ist {
  181. unsigned long ist[7];
  182. };
  183. DECLARE_PER_CPU(struct orig_ist, orig_ist);
  184. #ifdef CONFIG_X86_VSMP
  185. #define ARCH_MIN_TASKALIGN (1 << INTERNODE_CACHE_SHIFT)
  186. #define ARCH_MIN_MMSTRUCT_ALIGN (1 << INTERNODE_CACHE_SHIFT)
  187. #else
  188. #define ARCH_MIN_TASKALIGN 16
  189. #define ARCH_MIN_MMSTRUCT_ALIGN 0
  190. #endif
  191. struct thread_struct {
  192. unsigned long sp0;
  193. unsigned long sp;
  194. unsigned long usersp; /* Copy from PDA */
  195. unsigned long fs;
  196. unsigned long gs;
  197. unsigned short es, ds, fsindex, gsindex;
  198. /* Hardware debugging registers */
  199. unsigned long debugreg0;
  200. unsigned long debugreg1;
  201. unsigned long debugreg2;
  202. unsigned long debugreg3;
  203. unsigned long debugreg6;
  204. unsigned long debugreg7;
  205. /* fault info */
  206. unsigned long cr2, trap_no, error_code;
  207. /* floating point info */
  208. union i387_union i387 __attribute__((aligned(16)));
  209. /* IO permissions. the bitmap could be moved into the GDT, that would make
  210. switch faster for a limited number of ioperm using tasks. -AK */
  211. int ioperm;
  212. unsigned long *io_bitmap_ptr;
  213. unsigned io_bitmap_max;
  214. /* MSR_IA32_DEBUGCTLMSR value to switch in if TIF_DEBUGCTLMSR is set. */
  215. unsigned long debugctlmsr;
  216. /* Debug Store - if not 0 points to a DS Save Area configuration;
  217. * goes into MSR_IA32_DS_AREA */
  218. unsigned long ds_area_msr;
  219. /* cached TLS descriptors. */
  220. u64 tls_array[GDT_ENTRY_TLS_ENTRIES];
  221. } __attribute__((aligned(16)));
  222. #define INIT_THREAD { \
  223. .sp0 = (unsigned long)&init_stack + sizeof(init_stack) \
  224. }
  225. #define INIT_TSS { \
  226. .sp0 = (unsigned long)&init_stack + sizeof(init_stack) \
  227. }
  228. #define INIT_MMAP \
  229. { &init_mm, 0, 0, NULL, PAGE_SHARED, VM_READ | VM_WRITE | VM_EXEC, 1, NULL, NULL }
  230. #define start_thread(regs,new_rip,new_rsp) do { \
  231. asm volatile("movl %0,%%fs; movl %0,%%es; movl %0,%%ds": :"r" (0)); \
  232. load_gs_index(0); \
  233. (regs)->ip = (new_rip); \
  234. (regs)->sp = (new_rsp); \
  235. write_pda(oldrsp, (new_rsp)); \
  236. (regs)->cs = __USER_CS; \
  237. (regs)->ss = __USER_DS; \
  238. (regs)->flags = 0x200; \
  239. set_fs(USER_DS); \
  240. } while(0)
  241. #define get_debugreg(var, register) \
  242. __asm__("movq %%db" #register ", %0" \
  243. :"=r" (var))
  244. #define set_debugreg(value, register) \
  245. __asm__("movq %0,%%db" #register \
  246. : /* no output */ \
  247. :"r" (value))
  248. struct task_struct;
  249. struct mm_struct;
  250. /* Free all resources held by a thread. */
  251. extern void release_thread(struct task_struct *);
  252. /* Prepare to copy thread state - unlazy all lazy status */
  253. extern void prepare_to_copy(struct task_struct *tsk);
  254. /*
  255. * create a kernel thread without removing it from tasklists
  256. */
  257. extern long kernel_thread(int (*fn)(void *), void * arg, unsigned long flags);
  258. /*
  259. * Return saved PC of a blocked thread.
  260. * What is this good for? it will be always the scheduler or ret_from_fork.
  261. */
  262. #define thread_saved_pc(t) (*(unsigned long *)((t)->thread.sp - 8))
  263. extern unsigned long get_wchan(struct task_struct *p);
  264. #define task_pt_regs(tsk) ((struct pt_regs *)(tsk)->thread.sp0 - 1)
  265. #define KSTK_EIP(tsk) (task_pt_regs(tsk)->ip)
  266. #define KSTK_ESP(tsk) -1 /* sorry. doesn't work for syscall. */
  267. struct microcode_header {
  268. unsigned int hdrver;
  269. unsigned int rev;
  270. unsigned int date;
  271. unsigned int sig;
  272. unsigned int cksum;
  273. unsigned int ldrver;
  274. unsigned int pf;
  275. unsigned int datasize;
  276. unsigned int totalsize;
  277. unsigned int reserved[3];
  278. };
  279. struct microcode {
  280. struct microcode_header hdr;
  281. unsigned int bits[0];
  282. };
  283. typedef struct microcode microcode_t;
  284. typedef struct microcode_header microcode_header_t;
  285. /* microcode format is extended from prescott processors */
  286. struct extended_signature {
  287. unsigned int sig;
  288. unsigned int pf;
  289. unsigned int cksum;
  290. };
  291. struct extended_sigtable {
  292. unsigned int count;
  293. unsigned int cksum;
  294. unsigned int reserved[3];
  295. struct extended_signature sigs[0];
  296. };
  297. #if defined(CONFIG_MPSC) || defined(CONFIG_MCORE2)
  298. #define ASM_NOP1 P6_NOP1
  299. #define ASM_NOP2 P6_NOP2
  300. #define ASM_NOP3 P6_NOP3
  301. #define ASM_NOP4 P6_NOP4
  302. #define ASM_NOP5 P6_NOP5
  303. #define ASM_NOP6 P6_NOP6
  304. #define ASM_NOP7 P6_NOP7
  305. #define ASM_NOP8 P6_NOP8
  306. #else
  307. #define ASM_NOP1 K8_NOP1
  308. #define ASM_NOP2 K8_NOP2
  309. #define ASM_NOP3 K8_NOP3
  310. #define ASM_NOP4 K8_NOP4
  311. #define ASM_NOP5 K8_NOP5
  312. #define ASM_NOP6 K8_NOP6
  313. #define ASM_NOP7 K8_NOP7
  314. #define ASM_NOP8 K8_NOP8
  315. #endif
  316. /* Opteron nops */
  317. #define K8_NOP1 ".byte 0x90\n"
  318. #define K8_NOP2 ".byte 0x66,0x90\n"
  319. #define K8_NOP3 ".byte 0x66,0x66,0x90\n"
  320. #define K8_NOP4 ".byte 0x66,0x66,0x66,0x90\n"
  321. #define K8_NOP5 K8_NOP3 K8_NOP2
  322. #define K8_NOP6 K8_NOP3 K8_NOP3
  323. #define K8_NOP7 K8_NOP4 K8_NOP3
  324. #define K8_NOP8 K8_NOP4 K8_NOP4
  325. /* P6 nops */
  326. /* uses eax dependencies (Intel-recommended choice) */
  327. #define P6_NOP1 ".byte 0x90\n"
  328. #define P6_NOP2 ".byte 0x66,0x90\n"
  329. #define P6_NOP3 ".byte 0x0f,0x1f,0x00\n"
  330. #define P6_NOP4 ".byte 0x0f,0x1f,0x40,0\n"
  331. #define P6_NOP5 ".byte 0x0f,0x1f,0x44,0x00,0\n"
  332. #define P6_NOP6 ".byte 0x66,0x0f,0x1f,0x44,0x00,0\n"
  333. #define P6_NOP7 ".byte 0x0f,0x1f,0x80,0,0,0,0\n"
  334. #define P6_NOP8 ".byte 0x0f,0x1f,0x84,0x00,0,0,0,0\n"
  335. #define ASM_NOP_MAX 8
  336. /* REP NOP (PAUSE) is a good thing to insert into busy-wait loops. */
  337. static inline void rep_nop(void)
  338. {
  339. __asm__ __volatile__("rep;nop": : :"memory");
  340. }
  341. /* Stop speculative execution */
  342. static inline void sync_core(void)
  343. {
  344. int tmp;
  345. asm volatile("cpuid" : "=a" (tmp) : "0" (1) : "ebx","ecx","edx","memory");
  346. }
  347. #define ARCH_HAS_PREFETCHW 1
  348. static inline void prefetchw(void *x)
  349. {
  350. alternative_input("prefetcht0 (%1)",
  351. "prefetchw (%1)",
  352. X86_FEATURE_3DNOW,
  353. "r" (x));
  354. }
  355. #define ARCH_HAS_SPINLOCK_PREFETCH 1
  356. #define spin_lock_prefetch(x) prefetchw(x)
  357. #define cpu_relax() rep_nop()
  358. static inline void __monitor(const void *eax, unsigned long ecx,
  359. unsigned long edx)
  360. {
  361. /* "monitor %eax,%ecx,%edx;" */
  362. asm volatile(
  363. ".byte 0x0f,0x01,0xc8;"
  364. : :"a" (eax), "c" (ecx), "d"(edx));
  365. }
  366. static inline void __mwait(unsigned long eax, unsigned long ecx)
  367. {
  368. /* "mwait %eax,%ecx;" */
  369. asm volatile(
  370. ".byte 0x0f,0x01,0xc9;"
  371. : :"a" (eax), "c" (ecx));
  372. }
  373. static inline void __sti_mwait(unsigned long eax, unsigned long ecx)
  374. {
  375. /* "mwait %eax,%ecx;" */
  376. asm volatile(
  377. "sti; .byte 0x0f,0x01,0xc9;"
  378. : :"a" (eax), "c" (ecx));
  379. }
  380. extern void mwait_idle_with_hints(unsigned long eax, unsigned long ecx);
  381. extern int force_mwait;
  382. extern void select_idle_routine(const struct cpuinfo_x86 *c);
  383. #define stack_current() \
  384. ({ \
  385. struct thread_info *ti; \
  386. asm("andq %%rsp,%0; ":"=r" (ti) : "0" (CURRENT_MASK)); \
  387. ti->task; \
  388. })
  389. #define cache_line_size() (boot_cpu_data.x86_cache_alignment)
  390. extern unsigned long boot_option_idle_override;
  391. /* Boot loader type from the setup header */
  392. extern int bootloader_type;
  393. #define HAVE_ARCH_PICK_MMAP_LAYOUT 1
  394. #endif /* __ASM_X86_64_PROCESSOR_H */