]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/amd64/include/cpufunc.h
MFC r363988:
[FreeBSD/stable/9.git] / sys / amd64 / include / cpufunc.h
1 /*-
2  * Copyright (c) 2003 Peter Wemm.
3  * Copyright (c) 1993 The Regents of the University of California.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 4. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32
33 /*
34  * Functions to provide access to special i386 instructions.
35  * This in included in sys/systm.h, and that file should be
36  * used in preference to this.
37  */
38
39 #ifndef _MACHINE_CPUFUNC_H_
40 #define _MACHINE_CPUFUNC_H_
41
42 #ifndef _SYS_CDEFS_H_
43 #error this file needs sys/cdefs.h as a prerequisite
44 #endif
45
46 struct region_descriptor;
47
48 #define readb(va)       (*(volatile uint8_t *) (va))
49 #define readw(va)       (*(volatile uint16_t *) (va))
50 #define readl(va)       (*(volatile uint32_t *) (va))
51 #define readq(va)       (*(volatile uint64_t *) (va))
52
53 #define writeb(va, d)   (*(volatile uint8_t *) (va) = (d))
54 #define writew(va, d)   (*(volatile uint16_t *) (va) = (d))
55 #define writel(va, d)   (*(volatile uint32_t *) (va) = (d))
56 #define writeq(va, d)   (*(volatile uint64_t *) (va) = (d))
57
58 #if defined(__GNUCLIKE_ASM) && defined(__CC_SUPPORTS___INLINE)
59
60 static __inline void
61 breakpoint(void)
62 {
63         __asm __volatile("int $3");
64 }
65
66 static __inline u_int
67 bsfl(u_int mask)
68 {
69         u_int   result;
70
71         __asm __volatile("bsfl %1,%0" : "=r" (result) : "rm" (mask));
72         return (result);
73 }
74
75 static __inline u_long
76 bsfq(u_long mask)
77 {
78         u_long  result;
79
80         __asm __volatile("bsfq %1,%0" : "=r" (result) : "rm" (mask));
81         return (result);
82 }
83
84 static __inline u_int
85 bsrl(u_int mask)
86 {
87         u_int   result;
88
89         __asm __volatile("bsrl %1,%0" : "=r" (result) : "rm" (mask));
90         return (result);
91 }
92
93 static __inline u_long
94 bsrq(u_long mask)
95 {
96         u_long  result;
97
98         __asm __volatile("bsrq %1,%0" : "=r" (result) : "rm" (mask));
99         return (result);
100 }
101
102 static __inline void
103 clflush(u_long addr)
104 {
105
106         __asm __volatile("clflush %0" : : "m" (*(char *)addr));
107 }
108
109 static __inline void
110 clts(void)
111 {
112
113         __asm __volatile("clts");
114 }
115
116 static __inline void
117 disable_intr(void)
118 {
119         __asm __volatile("cli" : : : "memory");
120 }
121
122 static __inline void
123 do_cpuid(u_int ax, u_int *p)
124 {
125         __asm __volatile("cpuid"
126                          : "=a" (p[0]), "=b" (p[1]), "=c" (p[2]), "=d" (p[3])
127                          :  "0" (ax));
128 }
129
130 static __inline void
131 cpuid_count(u_int ax, u_int cx, u_int *p)
132 {
133         __asm __volatile("cpuid"
134                          : "=a" (p[0]), "=b" (p[1]), "=c" (p[2]), "=d" (p[3])
135                          :  "0" (ax), "c" (cx));
136 }
137
138 static __inline void
139 enable_intr(void)
140 {
141         __asm __volatile("sti");
142 }
143
144 #ifdef _KERNEL
145
146 #define HAVE_INLINE_FFS
147 #define        ffs(x)  __builtin_ffs(x)
148
149 #define HAVE_INLINE_FFSL
150
151 static __inline int
152 ffsl(long mask)
153 {
154         return (mask == 0 ? mask : (int)bsfq((u_long)mask) + 1);
155 }
156
157 #define HAVE_INLINE_FFSLL
158
159 static __inline int
160 ffsll(long long mask)
161 {
162         return (ffsl((long)mask));
163 }
164
165 #define HAVE_INLINE_FLS
166
167 static __inline int
168 fls(int mask)
169 {
170         return (mask == 0 ? mask : (int)bsrl((u_int)mask) + 1);
171 }
172
173 #define HAVE_INLINE_FLSL
174
175 static __inline int
176 flsl(long mask)
177 {
178         return (mask == 0 ? mask : (int)bsrq((u_long)mask) + 1);
179 }
180
181 #define HAVE_INLINE_FLSLL
182
183 static __inline int
184 flsll(long long mask)
185 {
186         return (flsl((long)mask));
187 }
188
189 #endif /* _KERNEL */
190
191 static __inline void
192 halt(void)
193 {
194         __asm __volatile("hlt");
195 }
196
197 static __inline u_char
198 inb(u_int port)
199 {
200         u_char  data;
201
202         __asm __volatile("inb %w1, %0" : "=a" (data) : "Nd" (port));
203         return (data);
204 }
205
206 static __inline u_int
207 inl(u_int port)
208 {
209         u_int   data;
210
211         __asm __volatile("inl %w1, %0" : "=a" (data) : "Nd" (port));
212         return (data);
213 }
214
215 static __inline void
216 insb(u_int port, void *addr, size_t count)
217 {
218         __asm __volatile("cld; rep; insb"
219                          : "+D" (addr), "+c" (count)
220                          : "d" (port)
221                          : "memory");
222 }
223
224 static __inline void
225 insw(u_int port, void *addr, size_t count)
226 {
227         __asm __volatile("cld; rep; insw"
228                          : "+D" (addr), "+c" (count)
229                          : "d" (port)
230                          : "memory");
231 }
232
233 static __inline void
234 insl(u_int port, void *addr, size_t count)
235 {
236         __asm __volatile("cld; rep; insl"
237                          : "+D" (addr), "+c" (count)
238                          : "d" (port)
239                          : "memory");
240 }
241
242 static __inline void
243 invd(void)
244 {
245         __asm __volatile("invd");
246 }
247
248 static __inline u_short
249 inw(u_int port)
250 {
251         u_short data;
252
253         __asm __volatile("inw %w1, %0" : "=a" (data) : "Nd" (port));
254         return (data);
255 }
256
257 static __inline void
258 outb(u_int port, u_char data)
259 {
260         __asm __volatile("outb %0, %w1" : : "a" (data), "Nd" (port));
261 }
262
263 static __inline void
264 outl(u_int port, u_int data)
265 {
266         __asm __volatile("outl %0, %w1" : : "a" (data), "Nd" (port));
267 }
268
269 static __inline void
270 outsb(u_int port, const void *addr, size_t count)
271 {
272         __asm __volatile("cld; rep; outsb"
273                          : "+S" (addr), "+c" (count)
274                          : "d" (port));
275 }
276
277 static __inline void
278 outsw(u_int port, const void *addr, size_t count)
279 {
280         __asm __volatile("cld; rep; outsw"
281                          : "+S" (addr), "+c" (count)
282                          : "d" (port));
283 }
284
285 static __inline void
286 outsl(u_int port, const void *addr, size_t count)
287 {
288         __asm __volatile("cld; rep; outsl"
289                          : "+S" (addr), "+c" (count)
290                          : "d" (port));
291 }
292
293 static __inline void
294 outw(u_int port, u_short data)
295 {
296         __asm __volatile("outw %0, %w1" : : "a" (data), "Nd" (port));
297 }
298
299 static __inline u_long
300 popcntq(u_long mask)
301 {
302         u_long result;
303
304         __asm __volatile("popcntq %1,%0" : "=r" (result) : "rm" (mask));
305         return (result);
306 }
307
308 static __inline void
309 lfence(void)
310 {
311
312         __asm __volatile("lfence" : : : "memory");
313 }
314
315 static __inline void
316 mfence(void)
317 {
318
319         __asm __volatile("mfence" : : : "memory");
320 }
321
322 static __inline void
323 ia32_pause(void)
324 {
325         __asm __volatile("pause");
326 }
327
328 static __inline u_long
329 read_rflags(void)
330 {
331         u_long  rf;
332
333         __asm __volatile("pushfq; popq %0" : "=r" (rf));
334         return (rf);
335 }
336
337 static __inline uint64_t
338 rdmsr(u_int msr)
339 {
340         uint32_t low, high;
341
342         __asm __volatile("rdmsr" : "=a" (low), "=d" (high) : "c" (msr));
343         return (low | ((uint64_t)high << 32));
344 }
345
346 static __inline uint64_t
347 rdpmc(u_int pmc)
348 {
349         uint32_t low, high;
350
351         __asm __volatile("rdpmc" : "=a" (low), "=d" (high) : "c" (pmc));
352         return (low | ((uint64_t)high << 32));
353 }
354
355 static __inline uint64_t
356 rdtsc(void)
357 {
358         uint32_t low, high;
359
360         __asm __volatile("rdtsc" : "=a" (low), "=d" (high));
361         return (low | ((uint64_t)high << 32));
362 }
363
364 static __inline uint32_t
365 rdtsc32(void)
366 {
367         uint32_t rv;
368
369         __asm __volatile("rdtsc" : "=a" (rv) : : "edx");
370         return (rv);
371 }
372
373 static __inline void
374 wbinvd(void)
375 {
376         __asm __volatile("wbinvd");
377 }
378
379 static __inline void
380 write_rflags(u_long rf)
381 {
382         __asm __volatile("pushq %0;  popfq" : : "r" (rf));
383 }
384
385 static __inline void
386 wrmsr(u_int msr, uint64_t newval)
387 {
388         uint32_t low, high;
389
390         low = newval;
391         high = newval >> 32;
392         __asm __volatile("wrmsr" : : "a" (low), "d" (high), "c" (msr));
393 }
394
395 static __inline void
396 load_cr0(u_long data)
397 {
398
399         __asm __volatile("movq %0,%%cr0" : : "r" (data));
400 }
401
402 static __inline u_long
403 rcr0(void)
404 {
405         u_long  data;
406
407         __asm __volatile("movq %%cr0,%0" : "=r" (data));
408         return (data);
409 }
410
411 static __inline u_long
412 rcr2(void)
413 {
414         u_long  data;
415
416         __asm __volatile("movq %%cr2,%0" : "=r" (data));
417         return (data);
418 }
419
420 static __inline void
421 load_cr3(u_long data)
422 {
423
424         __asm __volatile("movq %0,%%cr3" : : "r" (data) : "memory");
425 }
426
427 static __inline u_long
428 rcr3(void)
429 {
430         u_long  data;
431
432         __asm __volatile("movq %%cr3,%0" : "=r" (data));
433         return (data);
434 }
435
436 static __inline void
437 load_cr4(u_long data)
438 {
439         __asm __volatile("movq %0,%%cr4" : : "r" (data));
440 }
441
442 static __inline u_long
443 rcr4(void)
444 {
445         u_long  data;
446
447         __asm __volatile("movq %%cr4,%0" : "=r" (data));
448         return (data);
449 }
450
451 static __inline u_long
452 rxcr(u_int reg)
453 {
454         u_int low, high;
455
456         __asm __volatile("xgetbv" : "=a" (low), "=d" (high) : "c" (reg));
457         return (low | ((uint64_t)high << 32));
458 }
459
460 static __inline void
461 load_xcr(u_int reg, u_long val)
462 {
463         u_int low, high;
464
465         low = val;
466         high = val >> 32;
467         __asm __volatile("xsetbv" : : "c" (reg), "a" (low), "d" (high));
468 }
469
470 /*
471  * Global TLB flush (except for thise for pages marked PG_G)
472  */
473 static __inline void
474 invltlb(void)
475 {
476
477         load_cr3(rcr3());
478 }
479
480 /*
481  * TLB flush for an individual page (even if it has PG_G).
482  * Only works on 486+ CPUs (i386 does not have PG_G).
483  */
484 static __inline void
485 invlpg(u_long addr)
486 {
487
488         __asm __volatile("invlpg %0" : : "m" (*(char *)addr) : "memory");
489 }
490
491 static __inline u_short
492 rfs(void)
493 {
494         u_short sel;
495         __asm __volatile("movw %%fs,%0" : "=rm" (sel));
496         return (sel);
497 }
498
499 static __inline u_short
500 rgs(void)
501 {
502         u_short sel;
503         __asm __volatile("movw %%gs,%0" : "=rm" (sel));
504         return (sel);
505 }
506
507 static __inline u_short
508 rss(void)
509 {
510         u_short sel;
511         __asm __volatile("movw %%ss,%0" : "=rm" (sel));
512         return (sel);
513 }
514
515 static __inline void
516 load_ds(u_short sel)
517 {
518         __asm __volatile("movw %0,%%ds" : : "rm" (sel));
519 }
520
521 static __inline void
522 load_es(u_short sel)
523 {
524         __asm __volatile("movw %0,%%es" : : "rm" (sel));
525 }
526
527 static __inline void
528 cpu_monitor(const void *addr, u_long extensions, u_int hints)
529 {
530
531         __asm __volatile("monitor"
532             : : "a" (addr), "c" (extensions), "d" (hints));
533 }
534
535 static __inline void
536 cpu_mwait(u_long extensions, u_int hints)
537 {
538
539         __asm __volatile("mwait" : : "a" (hints), "c" (extensions));
540 }
541
542 #ifdef _KERNEL
543 /* This is defined in <machine/specialreg.h> but is too painful to get to */
544 #ifndef MSR_FSBASE
545 #define MSR_FSBASE      0xc0000100
546 #endif
547 static __inline void
548 load_fs(u_short sel)
549 {
550         /* Preserve the fsbase value across the selector load */
551         __asm __volatile("rdmsr; movw %0,%%fs; wrmsr"
552             : : "rm" (sel), "c" (MSR_FSBASE) : "eax", "edx");
553 }
554
555 #ifndef MSR_GSBASE
556 #define MSR_GSBASE      0xc0000101
557 #endif
558 static __inline void
559 load_gs(u_short sel)
560 {
561         /*
562          * Preserve the gsbase value across the selector load.
563          * Note that we have to disable interrupts because the gsbase
564          * being trashed happens to be the kernel gsbase at the time.
565          */
566         __asm __volatile("pushfq; cli; rdmsr; movw %0,%%gs; wrmsr; popfq"
567             : : "rm" (sel), "c" (MSR_GSBASE) : "eax", "edx");
568 }
569 #else
570 /* Usable by userland */
571 static __inline void
572 load_fs(u_short sel)
573 {
574         __asm __volatile("movw %0,%%fs" : : "rm" (sel));
575 }
576
577 static __inline void
578 load_gs(u_short sel)
579 {
580         __asm __volatile("movw %0,%%gs" : : "rm" (sel));
581 }
582 #endif
583
584 static __inline void
585 lidt(struct region_descriptor *addr)
586 {
587         __asm __volatile("lidt (%0)" : : "r" (addr));
588 }
589
590 static __inline void
591 lldt(u_short sel)
592 {
593         __asm __volatile("lldt %0" : : "r" (sel));
594 }
595
596 static __inline void
597 ltr(u_short sel)
598 {
599         __asm __volatile("ltr %0" : : "r" (sel));
600 }
601
602 static __inline uint64_t
603 rdr0(void)
604 {
605         uint64_t data;
606         __asm __volatile("movq %%dr0,%0" : "=r" (data));
607         return (data);
608 }
609
610 static __inline void
611 load_dr0(uint64_t dr0)
612 {
613         __asm __volatile("movq %0,%%dr0" : : "r" (dr0));
614 }
615
616 static __inline uint64_t
617 rdr1(void)
618 {
619         uint64_t data;
620         __asm __volatile("movq %%dr1,%0" : "=r" (data));
621         return (data);
622 }
623
624 static __inline void
625 load_dr1(uint64_t dr1)
626 {
627         __asm __volatile("movq %0,%%dr1" : : "r" (dr1));
628 }
629
630 static __inline uint64_t
631 rdr2(void)
632 {
633         uint64_t data;
634         __asm __volatile("movq %%dr2,%0" : "=r" (data));
635         return (data);
636 }
637
638 static __inline void
639 load_dr2(uint64_t dr2)
640 {
641         __asm __volatile("movq %0,%%dr2" : : "r" (dr2));
642 }
643
644 static __inline uint64_t
645 rdr3(void)
646 {
647         uint64_t data;
648         __asm __volatile("movq %%dr3,%0" : "=r" (data));
649         return (data);
650 }
651
652 static __inline void
653 load_dr3(uint64_t dr3)
654 {
655         __asm __volatile("movq %0,%%dr3" : : "r" (dr3));
656 }
657
658 static __inline uint64_t
659 rdr4(void)
660 {
661         uint64_t data;
662         __asm __volatile("movq %%dr4,%0" : "=r" (data));
663         return (data);
664 }
665
666 static __inline void
667 load_dr4(uint64_t dr4)
668 {
669         __asm __volatile("movq %0,%%dr4" : : "r" (dr4));
670 }
671
672 static __inline uint64_t
673 rdr5(void)
674 {
675         uint64_t data;
676         __asm __volatile("movq %%dr5,%0" : "=r" (data));
677         return (data);
678 }
679
680 static __inline void
681 load_dr5(uint64_t dr5)
682 {
683         __asm __volatile("movq %0,%%dr5" : : "r" (dr5));
684 }
685
686 static __inline uint64_t
687 rdr6(void)
688 {
689         uint64_t data;
690         __asm __volatile("movq %%dr6,%0" : "=r" (data));
691         return (data);
692 }
693
694 static __inline void
695 load_dr6(uint64_t dr6)
696 {
697         __asm __volatile("movq %0,%%dr6" : : "r" (dr6));
698 }
699
700 static __inline uint64_t
701 rdr7(void)
702 {
703         uint64_t data;
704         __asm __volatile("movq %%dr7,%0" : "=r" (data));
705         return (data);
706 }
707
708 static __inline void
709 load_dr7(uint64_t dr7)
710 {
711         __asm __volatile("movq %0,%%dr7" : : "r" (dr7));
712 }
713
714 static __inline register_t
715 intr_disable(void)
716 {
717         register_t rflags;
718
719         rflags = read_rflags();
720         disable_intr();
721         return (rflags);
722 }
723
724 static __inline void
725 intr_restore(register_t rflags)
726 {
727         write_rflags(rflags);
728 }
729
730 #else /* !(__GNUCLIKE_ASM && __CC_SUPPORTS___INLINE) */
731
732 int     breakpoint(void);
733 u_int   bsfl(u_int mask);
734 u_int   bsrl(u_int mask);
735 void    clflush(u_long addr);
736 void    clts(void);
737 void    cpuid_count(u_int ax, u_int cx, u_int *p);
738 void    disable_intr(void);
739 void    do_cpuid(u_int ax, u_int *p);
740 void    enable_intr(void);
741 void    halt(void);
742 void    ia32_pause(void);
743 u_char  inb(u_int port);
744 u_int   inl(u_int port);
745 void    insb(u_int port, void *addr, size_t count);
746 void    insl(u_int port, void *addr, size_t count);
747 void    insw(u_int port, void *addr, size_t count);
748 register_t      intr_disable(void);
749 void    intr_restore(register_t rf);
750 void    invd(void);
751 void    invlpg(u_int addr);
752 void    invltlb(void);
753 u_short inw(u_int port);
754 void    lidt(struct region_descriptor *addr);
755 void    lldt(u_short sel);
756 void    load_cr0(u_long cr0);
757 void    load_cr3(u_long cr3);
758 void    load_cr4(u_long cr4);
759 void    load_dr0(uint64_t dr0);
760 void    load_dr1(uint64_t dr1);
761 void    load_dr2(uint64_t dr2);
762 void    load_dr3(uint64_t dr3);
763 void    load_dr4(uint64_t dr4);
764 void    load_dr5(uint64_t dr5);
765 void    load_dr6(uint64_t dr6);
766 void    load_dr7(uint64_t dr7);
767 void    load_fs(u_short sel);
768 void    load_gs(u_short sel);
769 void    ltr(u_short sel);
770 void    outb(u_int port, u_char data);
771 void    outl(u_int port, u_int data);
772 void    outsb(u_int port, const void *addr, size_t count);
773 void    outsl(u_int port, const void *addr, size_t count);
774 void    outsw(u_int port, const void *addr, size_t count);
775 void    outw(u_int port, u_short data);
776 u_long  rcr0(void);
777 u_long  rcr2(void);
778 u_long  rcr3(void);
779 u_long  rcr4(void);
780 uint64_t rdmsr(u_int msr);
781 uint64_t rdpmc(u_int pmc);
782 uint64_t rdr0(void);
783 uint64_t rdr1(void);
784 uint64_t rdr2(void);
785 uint64_t rdr3(void);
786 uint64_t rdr4(void);
787 uint64_t rdr5(void);
788 uint64_t rdr6(void);
789 uint64_t rdr7(void);
790 uint64_t rdtsc(void);
791 u_int   read_rflags(void);
792 u_int   rfs(void);
793 u_int   rgs(void);
794 void    wbinvd(void);
795 void    write_rflags(u_int rf);
796 void    wrmsr(u_int msr, uint64_t newval);
797
798 #endif  /* __GNUCLIKE_ASM && __CC_SUPPORTS___INLINE */
799
800 void    reset_dbregs(void);
801
802 #ifdef _KERNEL
803 int     rdmsr_safe(u_int msr, uint64_t *val);
804 int     wrmsr_safe(u_int msr, uint64_t newval);
805 #endif
806
807 #endif /* !_MACHINE_CPUFUNC_H_ */