processor.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. #ifndef __ASM_X86_PROCESSOR_H
  2. #define __ASM_X86_PROCESSOR_H
  3. #include <asm/processor-flags.h>
  4. /* migration helpers, for KVM - will be removed in 2.6.25: */
  5. #include <asm/vm86.h>
  6. #define Xgt_desc_struct desc_ptr
  7. /* Forward declaration, a strange C thing */
  8. struct task_struct;
  9. struct mm_struct;
  10. #include <asm/vm86.h>
  11. #include <asm/math_emu.h>
  12. #include <asm/segment.h>
  13. #include <asm/types.h>
  14. #include <asm/sigcontext.h>
  15. #include <asm/current.h>
  16. #include <asm/cpufeature.h>
  17. #include <asm/system.h>
  18. #include <asm/page.h>
  19. #include <asm/percpu.h>
  20. #include <asm/msr.h>
  21. #include <asm/desc_defs.h>
  22. #include <asm/nops.h>
  23. #include <linux/personality.h>
  24. #include <linux/cpumask.h>
  25. #include <linux/cache.h>
  26. #include <linux/threads.h>
  27. #include <linux/init.h>
  28. /*
  29. * Default implementation of macro that returns current
  30. * instruction pointer ("program counter").
  31. */
  32. static inline void *current_text_addr(void)
  33. {
  34. void *pc;
  35. asm volatile("mov $1f, %0; 1:":"=r" (pc));
  36. return pc;
  37. }
  38. #ifdef CONFIG_X86_VSMP
  39. # define ARCH_MIN_TASKALIGN (1 << INTERNODE_CACHE_SHIFT)
  40. # define ARCH_MIN_MMSTRUCT_ALIGN (1 << INTERNODE_CACHE_SHIFT)
  41. #else
  42. # define ARCH_MIN_TASKALIGN 16
  43. # define ARCH_MIN_MMSTRUCT_ALIGN 0
  44. #endif
  45. /*
  46. * CPU type and hardware bug flags. Kept separately for each CPU.
  47. * Members of this structure are referenced in head.S, so think twice
  48. * before touching them. [mj]
  49. */
  50. struct cpuinfo_x86 {
  51. __u8 x86; /* CPU family */
  52. __u8 x86_vendor; /* CPU vendor */
  53. __u8 x86_model;
  54. __u8 x86_mask;
  55. #ifdef CONFIG_X86_32
  56. char wp_works_ok; /* It doesn't on 386's */
  57. /* Problems on some 486Dx4's and old 386's: */
  58. char hlt_works_ok;
  59. char hard_math;
  60. char rfu;
  61. char fdiv_bug;
  62. char f00f_bug;
  63. char coma_bug;
  64. char pad0;
  65. #else
  66. /* Number of 4K pages in DTLB/ITLB combined(in pages): */
  67. int x86_tlbsize;
  68. __u8 x86_virt_bits;
  69. __u8 x86_phys_bits;
  70. /* CPUID returned core id bits: */
  71. __u8 x86_coreid_bits;
  72. /* Max extended CPUID function supported: */
  73. __u32 extended_cpuid_level;
  74. #endif
  75. /* Maximum supported CPUID level, -1=no CPUID: */
  76. int cpuid_level;
  77. __u32 x86_capability[NCAPINTS];
  78. char x86_vendor_id[16];
  79. char x86_model_id[64];
  80. /* in KB - valid for CPUS which support this call: */
  81. int x86_cache_size;
  82. int x86_cache_alignment; /* In bytes */
  83. int x86_power;
  84. unsigned long loops_per_jiffy;
  85. #ifdef CONFIG_SMP
  86. /* cpus sharing the last level cache: */
  87. cpumask_t llc_shared_map;
  88. #endif
  89. /* cpuid returned max cores value: */
  90. u16 x86_max_cores;
  91. u16 apicid;
  92. u16 initial_apicid;
  93. u16 x86_clflush_size;
  94. #ifdef CONFIG_SMP
  95. /* number of cores as seen by the OS: */
  96. u16 booted_cores;
  97. /* Physical processor id: */
  98. u16 phys_proc_id;
  99. /* Core id: */
  100. u16 cpu_core_id;
  101. /* Index into per_cpu list: */
  102. u16 cpu_index;
  103. #endif
  104. } __attribute__((__aligned__(SMP_CACHE_BYTES)));
  105. #define X86_VENDOR_INTEL 0
  106. #define X86_VENDOR_CYRIX 1
  107. #define X86_VENDOR_AMD 2
  108. #define X86_VENDOR_UMC 3
  109. #define X86_VENDOR_NEXGEN 4
  110. #define X86_VENDOR_CENTAUR 5
  111. #define X86_VENDOR_TRANSMETA 7
  112. #define X86_VENDOR_NSC 8
  113. #define X86_VENDOR_NUM 9
  114. #define X86_VENDOR_UNKNOWN 0xff
  115. /*
  116. * capabilities of CPUs
  117. */
  118. extern struct cpuinfo_x86 boot_cpu_data;
  119. extern struct cpuinfo_x86 new_cpu_data;
  120. extern struct tss_struct doublefault_tss;
  121. extern __u32 cleared_cpu_caps[NCAPINTS];
  122. #ifdef CONFIG_SMP
  123. DECLARE_PER_CPU(struct cpuinfo_x86, cpu_info);
  124. #define cpu_data(cpu) per_cpu(cpu_info, cpu)
  125. #define current_cpu_data cpu_data(smp_processor_id())
  126. #else
  127. #define cpu_data(cpu) boot_cpu_data
  128. #define current_cpu_data boot_cpu_data
  129. #endif
  130. static inline int hlt_works(int cpu)
  131. {
  132. #ifdef CONFIG_X86_32
  133. return cpu_data(cpu).hlt_works_ok;
  134. #else
  135. return 1;
  136. #endif
  137. }
  138. #define cache_line_size() (boot_cpu_data.x86_cache_alignment)
  139. extern void cpu_detect(struct cpuinfo_x86 *c);
  140. extern void identify_cpu(struct cpuinfo_x86 *);
  141. extern void identify_boot_cpu(void);
  142. extern void identify_secondary_cpu(struct cpuinfo_x86 *);
  143. extern void print_cpu_info(struct cpuinfo_x86 *);
  144. extern void init_scattered_cpuid_features(struct cpuinfo_x86 *c);
  145. extern unsigned int init_intel_cacheinfo(struct cpuinfo_x86 *c);
  146. extern unsigned short num_cache_leaves;
  147. #if defined(CONFIG_X86_HT) || defined(CONFIG_X86_64)
  148. extern void detect_ht(struct cpuinfo_x86 *c);
  149. #else
  150. static inline void detect_ht(struct cpuinfo_x86 *c) {}
  151. #endif
  152. static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
  153. unsigned int *ecx, unsigned int *edx)
  154. {
  155. /* ecx is often an input as well as an output. */
  156. __asm__("cpuid"
  157. : "=a" (*eax),
  158. "=b" (*ebx),
  159. "=c" (*ecx),
  160. "=d" (*edx)
  161. : "0" (*eax), "2" (*ecx));
  162. }
  163. static inline void load_cr3(pgd_t *pgdir)
  164. {
  165. write_cr3(__pa(pgdir));
  166. }
  167. #ifdef CONFIG_X86_32
  168. /* This is the TSS defined by the hardware. */
  169. struct x86_hw_tss {
  170. unsigned short back_link, __blh;
  171. unsigned long sp0;
  172. unsigned short ss0, __ss0h;
  173. unsigned long sp1;
  174. /* ss1 caches MSR_IA32_SYSENTER_CS: */
  175. unsigned short ss1, __ss1h;
  176. unsigned long sp2;
  177. unsigned short ss2, __ss2h;
  178. unsigned long __cr3;
  179. unsigned long ip;
  180. unsigned long flags;
  181. unsigned long ax;
  182. unsigned long cx;
  183. unsigned long dx;
  184. unsigned long bx;
  185. unsigned long sp;
  186. unsigned long bp;
  187. unsigned long si;
  188. unsigned long di;
  189. unsigned short es, __esh;
  190. unsigned short cs, __csh;
  191. unsigned short ss, __ssh;
  192. unsigned short ds, __dsh;
  193. unsigned short fs, __fsh;
  194. unsigned short gs, __gsh;
  195. unsigned short ldt, __ldth;
  196. unsigned short trace;
  197. unsigned short io_bitmap_base;
  198. } __attribute__((packed));
  199. #else
  200. struct x86_hw_tss {
  201. u32 reserved1;
  202. u64 sp0;
  203. u64 sp1;
  204. u64 sp2;
  205. u64 reserved2;
  206. u64 ist[7];
  207. u32 reserved3;
  208. u32 reserved4;
  209. u16 reserved5;
  210. u16 io_bitmap_base;
  211. } __attribute__((packed)) ____cacheline_aligned;
  212. #endif
  213. /*
  214. * IO-bitmap sizes:
  215. */
  216. #define IO_BITMAP_BITS 65536
  217. #define IO_BITMAP_BYTES (IO_BITMAP_BITS/8)
  218. #define IO_BITMAP_LONGS (IO_BITMAP_BYTES/sizeof(long))
  219. #define IO_BITMAP_OFFSET offsetof(struct tss_struct, io_bitmap)
  220. #define INVALID_IO_BITMAP_OFFSET 0x8000
  221. #define INVALID_IO_BITMAP_OFFSET_LAZY 0x9000
  222. struct tss_struct {
  223. /*
  224. * The hardware state:
  225. */
  226. struct x86_hw_tss x86_tss;
  227. /*
  228. * The extra 1 is there because the CPU will access an
  229. * additional byte beyond the end of the IO permission
  230. * bitmap. The extra byte must be all 1 bits, and must
  231. * be within the limit.
  232. */
  233. unsigned long io_bitmap[IO_BITMAP_LONGS + 1];
  234. /*
  235. * Cache the current maximum and the last task that used the bitmap:
  236. */
  237. unsigned long io_bitmap_max;
  238. struct thread_struct *io_bitmap_owner;
  239. /*
  240. * Pad the TSS to be cacheline-aligned (size is 0x100):
  241. */
  242. unsigned long __cacheline_filler[35];
  243. /*
  244. * .. and then another 0x100 bytes for the emergency kernel stack:
  245. */
  246. unsigned long stack[64];
  247. } __attribute__((packed));
  248. DECLARE_PER_CPU(struct tss_struct, init_tss);
  249. /*
  250. * Save the original ist values for checking stack pointers during debugging
  251. */
  252. struct orig_ist {
  253. unsigned long ist[7];
  254. };
  255. #define MXCSR_DEFAULT 0x1f80
  256. struct i387_fsave_struct {
  257. u32 cwd; /* FPU Control Word */
  258. u32 swd; /* FPU Status Word */
  259. u32 twd; /* FPU Tag Word */
  260. u32 fip; /* FPU IP Offset */
  261. u32 fcs; /* FPU IP Selector */
  262. u32 foo; /* FPU Operand Pointer Offset */
  263. u32 fos; /* FPU Operand Pointer Selector */
  264. /* 8*10 bytes for each FP-reg = 80 bytes: */
  265. u32 st_space[20];
  266. /* Software status information [not touched by FSAVE ]: */
  267. u32 status;
  268. };
  269. struct i387_fxsave_struct {
  270. u16 cwd; /* Control Word */
  271. u16 swd; /* Status Word */
  272. u16 twd; /* Tag Word */
  273. u16 fop; /* Last Instruction Opcode */
  274. union {
  275. struct {
  276. u64 rip; /* Instruction Pointer */
  277. u64 rdp; /* Data Pointer */
  278. };
  279. struct {
  280. u32 fip; /* FPU IP Offset */
  281. u32 fcs; /* FPU IP Selector */
  282. u32 foo; /* FPU Operand Offset */
  283. u32 fos; /* FPU Operand Selector */
  284. };
  285. };
  286. u32 mxcsr; /* MXCSR Register State */
  287. u32 mxcsr_mask; /* MXCSR Mask */
  288. /* 8*16 bytes for each FP-reg = 128 bytes: */
  289. u32 st_space[32];
  290. /* 16*16 bytes for each XMM-reg = 256 bytes: */
  291. u32 xmm_space[64];
  292. u32 padding[24];
  293. } __attribute__((aligned(16)));
  294. struct i387_soft_struct {
  295. u32 cwd;
  296. u32 swd;
  297. u32 twd;
  298. u32 fip;
  299. u32 fcs;
  300. u32 foo;
  301. u32 fos;
  302. /* 8*10 bytes for each FP-reg = 80 bytes: */
  303. u32 st_space[20];
  304. u8 ftop;
  305. u8 changed;
  306. u8 lookahead;
  307. u8 no_update;
  308. u8 rm;
  309. u8 alimit;
  310. struct info *info;
  311. u32 entry_eip;
  312. };
  313. union i387_union {
  314. struct i387_fsave_struct fsave;
  315. struct i387_fxsave_struct fxsave;
  316. struct i387_soft_struct soft;
  317. };
  318. #ifdef CONFIG_X86_64
  319. DECLARE_PER_CPU(struct orig_ist, orig_ist);
  320. #endif
  321. extern void print_cpu_info(struct cpuinfo_x86 *);
  322. extern void init_scattered_cpuid_features(struct cpuinfo_x86 *c);
  323. extern unsigned int init_intel_cacheinfo(struct cpuinfo_x86 *c);
  324. extern unsigned short num_cache_leaves;
  325. struct thread_struct {
  326. /* Cached TLS descriptors: */
  327. struct desc_struct tls_array[GDT_ENTRY_TLS_ENTRIES];
  328. unsigned long sp0;
  329. unsigned long sp;
  330. #ifdef CONFIG_X86_32
  331. unsigned long sysenter_cs;
  332. #else
  333. unsigned long usersp; /* Copy from PDA */
  334. unsigned short es;
  335. unsigned short ds;
  336. unsigned short fsindex;
  337. unsigned short gsindex;
  338. #endif
  339. unsigned long ip;
  340. unsigned long fs;
  341. unsigned long gs;
  342. /* Hardware debugging registers: */
  343. unsigned long debugreg0;
  344. unsigned long debugreg1;
  345. unsigned long debugreg2;
  346. unsigned long debugreg3;
  347. unsigned long debugreg6;
  348. unsigned long debugreg7;
  349. /* Fault info: */
  350. unsigned long cr2;
  351. unsigned long trap_no;
  352. unsigned long error_code;
  353. /* Floating point info: */
  354. union i387_union i387 __attribute__((aligned(16)));;
  355. #ifdef CONFIG_X86_32
  356. /* Virtual 86 mode info */
  357. struct vm86_struct __user *vm86_info;
  358. unsigned long screen_bitmap;
  359. unsigned long v86flags;
  360. unsigned long v86mask;
  361. unsigned long saved_sp0;
  362. unsigned int saved_fs;
  363. unsigned int saved_gs;
  364. #endif
  365. /* IO permissions: */
  366. unsigned long *io_bitmap_ptr;
  367. unsigned long iopl;
  368. /* Max allowed port in the bitmap, in bytes: */
  369. unsigned io_bitmap_max;
  370. /* MSR_IA32_DEBUGCTLMSR value to switch in if TIF_DEBUGCTLMSR is set. */
  371. unsigned long debugctlmsr;
  372. /* Debug Store - if not 0 points to a DS Save Area configuration;
  373. * goes into MSR_IA32_DS_AREA */
  374. unsigned long ds_area_msr;
  375. };
  376. static inline unsigned long native_get_debugreg(int regno)
  377. {
  378. unsigned long val = 0; /* Damn you, gcc! */
  379. switch (regno) {
  380. case 0:
  381. asm("mov %%db0, %0" :"=r" (val)); break;
  382. case 1:
  383. asm("mov %%db1, %0" :"=r" (val)); break;
  384. case 2:
  385. asm("mov %%db2, %0" :"=r" (val)); break;
  386. case 3:
  387. asm("mov %%db3, %0" :"=r" (val)); break;
  388. case 6:
  389. asm("mov %%db6, %0" :"=r" (val)); break;
  390. case 7:
  391. asm("mov %%db7, %0" :"=r" (val)); break;
  392. default:
  393. BUG();
  394. }
  395. return val;
  396. }
  397. static inline void native_set_debugreg(int regno, unsigned long value)
  398. {
  399. switch (regno) {
  400. case 0:
  401. asm("mov %0, %%db0" ::"r" (value));
  402. break;
  403. case 1:
  404. asm("mov %0, %%db1" ::"r" (value));
  405. break;
  406. case 2:
  407. asm("mov %0, %%db2" ::"r" (value));
  408. break;
  409. case 3:
  410. asm("mov %0, %%db3" ::"r" (value));
  411. break;
  412. case 6:
  413. asm("mov %0, %%db6" ::"r" (value));
  414. break;
  415. case 7:
  416. asm("mov %0, %%db7" ::"r" (value));
  417. break;
  418. default:
  419. BUG();
  420. }
  421. }
  422. /*
  423. * Set IOPL bits in EFLAGS from given mask
  424. */
  425. static inline void native_set_iopl_mask(unsigned mask)
  426. {
  427. #ifdef CONFIG_X86_32
  428. unsigned int reg;
  429. __asm__ __volatile__ ("pushfl;"
  430. "popl %0;"
  431. "andl %1, %0;"
  432. "orl %2, %0;"
  433. "pushl %0;"
  434. "popfl"
  435. : "=&r" (reg)
  436. : "i" (~X86_EFLAGS_IOPL), "r" (mask));
  437. #endif
  438. }
  439. static inline void
  440. native_load_sp0(struct tss_struct *tss, struct thread_struct *thread)
  441. {
  442. tss->x86_tss.sp0 = thread->sp0;
  443. #ifdef CONFIG_X86_32
  444. /* Only happens when SEP is enabled, no need to test "SEP"arately: */
  445. if (unlikely(tss->x86_tss.ss1 != thread->sysenter_cs)) {
  446. tss->x86_tss.ss1 = thread->sysenter_cs;
  447. wrmsr(MSR_IA32_SYSENTER_CS, thread->sysenter_cs, 0);
  448. }
  449. #endif
  450. }
  451. static inline void native_swapgs(void)
  452. {
  453. #ifdef CONFIG_X86_64
  454. asm volatile("swapgs" ::: "memory");
  455. #endif
  456. }
  457. #ifdef CONFIG_PARAVIRT
  458. #include <asm/paravirt.h>
  459. #else
  460. #define __cpuid native_cpuid
  461. #define paravirt_enabled() 0
  462. /*
  463. * These special macros can be used to get or set a debugging register
  464. */
  465. #define get_debugreg(var, register) \
  466. (var) = native_get_debugreg(register)
  467. #define set_debugreg(value, register) \
  468. native_set_debugreg(register, value)
  469. static inline void
  470. load_sp0(struct tss_struct *tss, struct thread_struct *thread)
  471. {
  472. native_load_sp0(tss, thread);
  473. }
  474. #define set_iopl_mask native_set_iopl_mask
  475. #define SWAPGS swapgs
  476. #endif /* CONFIG_PARAVIRT */
  477. /*
  478. * Save the cr4 feature set we're using (ie
  479. * Pentium 4MB enable and PPro Global page
  480. * enable), so that any CPU's that boot up
  481. * after us can get the correct flags.
  482. */
  483. extern unsigned long mmu_cr4_features;
  484. static inline void set_in_cr4(unsigned long mask)
  485. {
  486. unsigned cr4;
  487. mmu_cr4_features |= mask;
  488. cr4 = read_cr4();
  489. cr4 |= mask;
  490. write_cr4(cr4);
  491. }
  492. static inline void clear_in_cr4(unsigned long mask)
  493. {
  494. unsigned cr4;
  495. mmu_cr4_features &= ~mask;
  496. cr4 = read_cr4();
  497. cr4 &= ~mask;
  498. write_cr4(cr4);
  499. }
  500. struct microcode_header {
  501. unsigned int hdrver;
  502. unsigned int rev;
  503. unsigned int date;
  504. unsigned int sig;
  505. unsigned int cksum;
  506. unsigned int ldrver;
  507. unsigned int pf;
  508. unsigned int datasize;
  509. unsigned int totalsize;
  510. unsigned int reserved[3];
  511. };
  512. struct microcode {
  513. struct microcode_header hdr;
  514. unsigned int bits[0];
  515. };
  516. typedef struct microcode microcode_t;
  517. typedef struct microcode_header microcode_header_t;
  518. /* microcode format is extended from prescott processors */
  519. struct extended_signature {
  520. unsigned int sig;
  521. unsigned int pf;
  522. unsigned int cksum;
  523. };
  524. struct extended_sigtable {
  525. unsigned int count;
  526. unsigned int cksum;
  527. unsigned int reserved[3];
  528. struct extended_signature sigs[0];
  529. };
  530. typedef struct {
  531. unsigned long seg;
  532. } mm_segment_t;
  533. /*
  534. * create a kernel thread without removing it from tasklists
  535. */
  536. extern int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags);
  537. /* Free all resources held by a thread. */
  538. extern void release_thread(struct task_struct *);
  539. /* Prepare to copy thread state - unlazy all lazy state */
  540. extern void prepare_to_copy(struct task_struct *tsk);
  541. unsigned long get_wchan(struct task_struct *p);
  542. /*
  543. * Generic CPUID function
  544. * clear %ecx since some cpus (Cyrix MII) do not set or clear %ecx
  545. * resulting in stale register contents being returned.
  546. */
  547. static inline void cpuid(unsigned int op,
  548. unsigned int *eax, unsigned int *ebx,
  549. unsigned int *ecx, unsigned int *edx)
  550. {
  551. *eax = op;
  552. *ecx = 0;
  553. __cpuid(eax, ebx, ecx, edx);
  554. }
  555. /* Some CPUID calls want 'count' to be placed in ecx */
  556. static inline void cpuid_count(unsigned int op, int count,
  557. unsigned int *eax, unsigned int *ebx,
  558. unsigned int *ecx, unsigned int *edx)
  559. {
  560. *eax = op;
  561. *ecx = count;
  562. __cpuid(eax, ebx, ecx, edx);
  563. }
  564. /*
  565. * CPUID functions returning a single datum
  566. */
  567. static inline unsigned int cpuid_eax(unsigned int op)
  568. {
  569. unsigned int eax, ebx, ecx, edx;
  570. cpuid(op, &eax, &ebx, &ecx, &edx);
  571. return eax;
  572. }
  573. static inline unsigned int cpuid_ebx(unsigned int op)
  574. {
  575. unsigned int eax, ebx, ecx, edx;
  576. cpuid(op, &eax, &ebx, &ecx, &edx);
  577. return ebx;
  578. }
  579. static inline unsigned int cpuid_ecx(unsigned int op)
  580. {
  581. unsigned int eax, ebx, ecx, edx;
  582. cpuid(op, &eax, &ebx, &ecx, &edx);
  583. return ecx;
  584. }
  585. static inline unsigned int cpuid_edx(unsigned int op)
  586. {
  587. unsigned int eax, ebx, ecx, edx;
  588. cpuid(op, &eax, &ebx, &ecx, &edx);
  589. return edx;
  590. }
  591. /* REP NOP (PAUSE) is a good thing to insert into busy-wait loops. */
  592. static inline void rep_nop(void)
  593. {
  594. __asm__ __volatile__("rep; nop" ::: "memory");
  595. }
  596. static inline void cpu_relax(void)
  597. {
  598. rep_nop();
  599. }
  600. /* Stop speculative execution: */
  601. static inline void sync_core(void)
  602. {
  603. int tmp;
  604. asm volatile("cpuid" : "=a" (tmp) : "0" (1)
  605. : "ebx", "ecx", "edx", "memory");
  606. }
  607. static inline void
  608. __monitor(const void *eax, unsigned long ecx, unsigned long edx)
  609. {
  610. /* "monitor %eax, %ecx, %edx;" */
  611. asm volatile(
  612. ".byte 0x0f, 0x01, 0xc8;"
  613. :: "a" (eax), "c" (ecx), "d"(edx));
  614. }
  615. static inline void __mwait(unsigned long eax, unsigned long ecx)
  616. {
  617. /* "mwait %eax, %ecx;" */
  618. asm volatile(
  619. ".byte 0x0f, 0x01, 0xc9;"
  620. :: "a" (eax), "c" (ecx));
  621. }
  622. static inline void __sti_mwait(unsigned long eax, unsigned long ecx)
  623. {
  624. /* "mwait %eax, %ecx;" */
  625. asm volatile(
  626. "sti; .byte 0x0f, 0x01, 0xc9;"
  627. :: "a" (eax), "c" (ecx));
  628. }
  629. extern void mwait_idle_with_hints(unsigned long eax, unsigned long ecx);
  630. extern int force_mwait;
  631. extern void select_idle_routine(const struct cpuinfo_x86 *c);
  632. extern unsigned long boot_option_idle_override;
  633. extern void enable_sep_cpu(void);
  634. extern int sysenter_setup(void);
  635. /* Defined in head.S */
  636. extern struct desc_ptr early_gdt_descr;
  637. extern void cpu_set_gdt(int);
  638. extern void switch_to_new_gdt(void);
  639. extern void cpu_init(void);
  640. extern void init_gdt(int cpu);
  641. /*
  642. * from system description table in BIOS. Mostly for MCA use, but
  643. * others may find it useful:
  644. */
  645. extern unsigned int machine_id;
  646. extern unsigned int machine_submodel_id;
  647. extern unsigned int BIOS_revision;
  648. /* Boot loader type from the setup header: */
  649. extern int bootloader_type;
  650. extern char ignore_fpu_irq;
  651. #define HAVE_ARCH_PICK_MMAP_LAYOUT 1
  652. #define ARCH_HAS_PREFETCHW
  653. #define ARCH_HAS_SPINLOCK_PREFETCH
  654. #ifdef CONFIG_X86_32
  655. # define BASE_PREFETCH ASM_NOP4
  656. # define ARCH_HAS_PREFETCH
  657. #else
  658. # define BASE_PREFETCH "prefetcht0 (%1)"
  659. #endif
  660. /*
  661. * Prefetch instructions for Pentium III (+) and AMD Athlon (+)
  662. *
  663. * It's not worth to care about 3dnow prefetches for the K6
  664. * because they are microcoded there and very slow.
  665. */
  666. static inline void prefetch(const void *x)
  667. {
  668. alternative_input(BASE_PREFETCH,
  669. "prefetchnta (%1)",
  670. X86_FEATURE_XMM,
  671. "r" (x));
  672. }
  673. /*
  674. * 3dnow prefetch to get an exclusive cache line.
  675. * Useful for spinlocks to avoid one state transition in the
  676. * cache coherency protocol:
  677. */
  678. static inline void prefetchw(const void *x)
  679. {
  680. alternative_input(BASE_PREFETCH,
  681. "prefetchw (%1)",
  682. X86_FEATURE_3DNOW,
  683. "r" (x));
  684. }
  685. static inline void spin_lock_prefetch(const void *x)
  686. {
  687. prefetchw(x);
  688. }
  689. #ifdef CONFIG_X86_32
  690. /*
  691. * User space process size: 3GB (default).
  692. */
  693. #define TASK_SIZE PAGE_OFFSET
  694. #define STACK_TOP TASK_SIZE
  695. #define STACK_TOP_MAX STACK_TOP
  696. #define INIT_THREAD { \
  697. .sp0 = sizeof(init_stack) + (long)&init_stack, \
  698. .vm86_info = NULL, \
  699. .sysenter_cs = __KERNEL_CS, \
  700. .io_bitmap_ptr = NULL, \
  701. .fs = __KERNEL_PERCPU, \
  702. }
  703. /*
  704. * Note that the .io_bitmap member must be extra-big. This is because
  705. * the CPU will access an additional byte beyond the end of the IO
  706. * permission bitmap. The extra byte must be all 1 bits, and must
  707. * be within the limit.
  708. */
  709. #define INIT_TSS { \
  710. .x86_tss = { \
  711. .sp0 = sizeof(init_stack) + (long)&init_stack, \
  712. .ss0 = __KERNEL_DS, \
  713. .ss1 = __KERNEL_CS, \
  714. .io_bitmap_base = INVALID_IO_BITMAP_OFFSET, \
  715. }, \
  716. .io_bitmap = { [0 ... IO_BITMAP_LONGS] = ~0 }, \
  717. }
  718. extern unsigned long thread_saved_pc(struct task_struct *tsk);
  719. #define THREAD_SIZE_LONGS (THREAD_SIZE/sizeof(unsigned long))
  720. #define KSTK_TOP(info) \
  721. ({ \
  722. unsigned long *__ptr = (unsigned long *)(info); \
  723. (unsigned long)(&__ptr[THREAD_SIZE_LONGS]); \
  724. })
  725. /*
  726. * The below -8 is to reserve 8 bytes on top of the ring0 stack.
  727. * This is necessary to guarantee that the entire "struct pt_regs"
  728. * is accessable even if the CPU haven't stored the SS/ESP registers
  729. * on the stack (interrupt gate does not save these registers
  730. * when switching to the same priv ring).
  731. * Therefore beware: accessing the ss/esp fields of the
  732. * "struct pt_regs" is possible, but they may contain the
  733. * completely wrong values.
  734. */
  735. #define task_pt_regs(task) \
  736. ({ \
  737. struct pt_regs *__regs__; \
  738. __regs__ = (struct pt_regs *)(KSTK_TOP(task_stack_page(task))-8); \
  739. __regs__ - 1; \
  740. })
  741. #define KSTK_ESP(task) (task_pt_regs(task)->sp)
  742. #else
  743. /*
  744. * User space process size. 47bits minus one guard page.
  745. */
  746. #define TASK_SIZE64 (0x800000000000UL - 4096)
  747. /* This decides where the kernel will search for a free chunk of vm
  748. * space during mmap's.
  749. */
  750. #define IA32_PAGE_OFFSET ((current->personality & ADDR_LIMIT_3GB) ? \
  751. 0xc0000000 : 0xFFFFe000)
  752. #define TASK_SIZE (test_thread_flag(TIF_IA32) ? \
  753. IA32_PAGE_OFFSET : TASK_SIZE64)
  754. #define TASK_SIZE_OF(child) ((test_tsk_thread_flag(child, TIF_IA32)) ? \
  755. IA32_PAGE_OFFSET : TASK_SIZE64)
  756. #define STACK_TOP TASK_SIZE
  757. #define STACK_TOP_MAX TASK_SIZE64
  758. #define INIT_THREAD { \
  759. .sp0 = (unsigned long)&init_stack + sizeof(init_stack) \
  760. }
  761. #define INIT_TSS { \
  762. .x86_tss.sp0 = (unsigned long)&init_stack + sizeof(init_stack) \
  763. }
  764. /*
  765. * Return saved PC of a blocked thread.
  766. * What is this good for? it will be always the scheduler or ret_from_fork.
  767. */
  768. #define thread_saved_pc(t) (*(unsigned long *)((t)->thread.sp - 8))
  769. #define task_pt_regs(tsk) ((struct pt_regs *)(tsk)->thread.sp0 - 1)
  770. #define KSTK_ESP(tsk) -1 /* sorry. doesn't work for syscall. */
  771. #endif /* CONFIG_X86_64 */
  772. extern void start_thread(struct pt_regs *regs, unsigned long new_ip,
  773. unsigned long new_sp);
  774. /*
  775. * This decides where the kernel will search for a free chunk of vm
  776. * space during mmap's.
  777. */
  778. #define TASK_UNMAPPED_BASE (PAGE_ALIGN(TASK_SIZE / 3))
  779. #define KSTK_EIP(task) (task_pt_regs(task)->ip)
  780. #endif