]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/amd64/include/cpufunc.h
MFC r362623:
[FreeBSD/stable/8.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 u_int8_t *) (va))
49 #define readw(va)       (*(volatile u_int16_t *) (va))
50 #define readl(va)       (*(volatile u_int32_t *) (va))
51 #define readq(va)       (*(volatile u_int64_t *) (va))
52
53 #define writeb(va, d)   (*(volatile u_int8_t *) (va) = (d))
54 #define writew(va, d)   (*(volatile u_int16_t *) (va) = (d))
55 #define writel(va, d)   (*(volatile u_int32_t *) (va) = (d))
56 #define writeq(va, d)   (*(volatile u_int64_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 disable_intr(void)
111 {
112         __asm __volatile("cli" : : : "memory");
113 }
114
115 static __inline void
116 do_cpuid(u_int ax, u_int *p)
117 {
118         __asm __volatile("cpuid"
119                          : "=a" (p[0]), "=b" (p[1]), "=c" (p[2]), "=d" (p[3])
120                          :  "0" (ax));
121 }
122
123 static __inline void
124 cpuid_count(u_int ax, u_int cx, u_int *p)
125 {
126         __asm __volatile("cpuid"
127                          : "=a" (p[0]), "=b" (p[1]), "=c" (p[2]), "=d" (p[3])
128                          :  "0" (ax), "c" (cx));
129 }
130
131 static __inline void
132 enable_intr(void)
133 {
134         __asm __volatile("sti");
135 }
136
137 #ifdef _KERNEL
138
139 #define HAVE_INLINE_FFS
140 #define        ffs(x)  __builtin_ffs(x)
141
142 #define HAVE_INLINE_FFSL
143
144 static __inline int
145 ffsl(long mask)
146 {
147         return (mask == 0 ? mask : (int)bsfq((u_long)mask) + 1);
148 }
149
150 #define HAVE_INLINE_FLS
151
152 static __inline int
153 fls(int mask)
154 {
155         return (mask == 0 ? mask : (int)bsrl((u_int)mask) + 1);
156 }
157
158 #define HAVE_INLINE_FLSL
159
160 static __inline int
161 flsl(long mask)
162 {
163         return (mask == 0 ? mask : (int)bsrq((u_long)mask) + 1);
164 }
165
166 #endif /* _KERNEL */
167
168 static __inline void
169 halt(void)
170 {
171         __asm __volatile("hlt");
172 }
173
174 static __inline u_char
175 inb(u_int port)
176 {
177         u_char  data;
178
179         __asm volatile("inb %w1, %0" : "=a" (data) : "Nd" (port));
180         return (data);
181 }
182
183 static __inline u_int
184 inl(u_int port)
185 {
186         u_int   data;
187
188         __asm volatile("inl %w1, %0" : "=a" (data) : "Nd" (port));
189         return (data);
190 }
191
192 static __inline void
193 insb(u_int port, void *addr, size_t cnt)
194 {
195         __asm __volatile("cld; rep; insb"
196                          : "+D" (addr), "+c" (cnt)
197                          : "d" (port)
198                          : "memory");
199 }
200
201 static __inline void
202 insw(u_int port, void *addr, size_t cnt)
203 {
204         __asm __volatile("cld; rep; insw"
205                          : "+D" (addr), "+c" (cnt)
206                          : "d" (port)
207                          : "memory");
208 }
209
210 static __inline void
211 insl(u_int port, void *addr, size_t cnt)
212 {
213         __asm __volatile("cld; rep; insl"
214                          : "+D" (addr), "+c" (cnt)
215                          : "d" (port)
216                          : "memory");
217 }
218
219 static __inline void
220 invd(void)
221 {
222         __asm __volatile("invd");
223 }
224
225 static __inline u_short
226 inw(u_int port)
227 {
228         u_short data;
229
230         __asm volatile("inw %w1, %0" : "=a" (data) : "Nd" (port));
231         return (data);
232 }
233
234 static __inline void
235 outb(u_int port, u_char data)
236 {
237         __asm volatile("outb %0, %w1" : : "a" (data), "Nd" (port));
238 }
239
240 static __inline void
241 outl(u_int port, u_int data)
242 {
243         __asm volatile("outl %0, %w1" : : "a" (data), "Nd" (port));
244 }
245
246 static __inline void
247 outsb(u_int port, const void *addr, size_t cnt)
248 {
249         __asm __volatile("cld; rep; outsb"
250                          : "+S" (addr), "+c" (cnt)
251                          : "d" (port));
252 }
253
254 static __inline void
255 outsw(u_int port, const void *addr, size_t cnt)
256 {
257         __asm __volatile("cld; rep; outsw"
258                          : "+S" (addr), "+c" (cnt)
259                          : "d" (port));
260 }
261
262 static __inline void
263 outsl(u_int port, const void *addr, size_t cnt)
264 {
265         __asm __volatile("cld; rep; outsl"
266                          : "+S" (addr), "+c" (cnt)
267                          : "d" (port));
268 }
269
270 static __inline void
271 outw(u_int port, u_short data)
272 {
273         __asm volatile("outw %0, %w1" : : "a" (data), "Nd" (port));
274 }
275
276 static __inline void
277 lfence(void)
278 {
279
280         __asm __volatile("lfence" : : : "memory");
281 }
282
283 static __inline void
284 mfence(void)
285 {
286
287         __asm __volatile("mfence" : : : "memory");
288 }
289
290 static __inline void
291 ia32_pause(void)
292 {
293         __asm __volatile("pause");
294 }
295
296 static __inline u_long
297 read_rflags(void)
298 {
299         u_long  rf;
300
301         __asm __volatile("pushfq; popq %0" : "=r" (rf));
302         return (rf);
303 }
304
305 static __inline u_int64_t
306 rdmsr(u_int msr)
307 {
308         u_int32_t low, high;
309
310         __asm __volatile("rdmsr" : "=a" (low), "=d" (high) : "c" (msr));
311         return (low | ((u_int64_t)high << 32));
312 }
313
314 static __inline u_int64_t
315 rdpmc(u_int pmc)
316 {
317         u_int32_t low, high;
318
319         __asm __volatile("rdpmc" : "=a" (low), "=d" (high) : "c" (pmc));
320         return (low | ((u_int64_t)high << 32));
321 }
322
323 static __inline u_int64_t
324 rdtsc(void)
325 {
326         u_int32_t low, high;
327
328         __asm __volatile("rdtsc" : "=a" (low), "=d" (high));
329         return (low | ((u_int64_t)high << 32));
330 }
331
332 static __inline void
333 wbinvd(void)
334 {
335         __asm __volatile("wbinvd");
336 }
337
338 static __inline void
339 write_rflags(u_long rf)
340 {
341         __asm __volatile("pushq %0;  popfq" : : "r" (rf));
342 }
343
344 static __inline void
345 wrmsr(u_int msr, u_int64_t newval)
346 {
347         u_int32_t low, high;
348
349         low = newval;
350         high = newval >> 32;
351         __asm __volatile("wrmsr" : : "a" (low), "d" (high), "c" (msr));
352 }
353
354 static __inline void
355 load_cr0(u_long data)
356 {
357
358         __asm __volatile("movq %0,%%cr0" : : "r" (data));
359 }
360
361 static __inline u_long
362 rcr0(void)
363 {
364         u_long  data;
365
366         __asm __volatile("movq %%cr0,%0" : "=r" (data));
367         return (data);
368 }
369
370 static __inline u_long
371 rcr2(void)
372 {
373         u_long  data;
374
375         __asm __volatile("movq %%cr2,%0" : "=r" (data));
376         return (data);
377 }
378
379 static __inline void
380 load_cr3(u_long data)
381 {
382
383         __asm __volatile("movq %0,%%cr3" : : "r" (data) : "memory");
384 }
385
386 static __inline u_long
387 rcr3(void)
388 {
389         u_long  data;
390
391         __asm __volatile("movq %%cr3,%0" : "=r" (data));
392         return (data);
393 }
394
395 static __inline void
396 load_cr4(u_long data)
397 {
398         __asm __volatile("movq %0,%%cr4" : : "r" (data));
399 }
400
401 static __inline u_long
402 rcr4(void)
403 {
404         u_long  data;
405
406         __asm __volatile("movq %%cr4,%0" : "=r" (data));
407         return (data);
408 }
409
410 /*
411  * Global TLB flush (except for thise for pages marked PG_G)
412  */
413 static __inline void
414 invltlb(void)
415 {
416
417         load_cr3(rcr3());
418 }
419
420 /*
421  * TLB flush for an individual page (even if it has PG_G).
422  * Only works on 486+ CPUs (i386 does not have PG_G).
423  */
424 static __inline void
425 invlpg(u_long addr)
426 {
427
428         __asm __volatile("invlpg %0" : : "m" (*(char *)addr) : "memory");
429 }
430
431 static __inline u_int
432 rfs(void)
433 {
434         u_int sel;
435         __asm __volatile("mov %%fs,%0" : "=rm" (sel));
436         return (sel);
437 }
438
439 static __inline u_int
440 rgs(void)
441 {
442         u_int sel;
443         __asm __volatile("mov %%gs,%0" : "=rm" (sel));
444         return (sel);
445 }
446
447 static __inline u_int
448 rss(void)
449 {
450         u_int sel;
451         __asm __volatile("mov %%ss,%0" : "=rm" (sel));
452         return (sel);
453 }
454
455 static __inline void
456 load_ds(u_int sel)
457 {
458         __asm __volatile("mov %0,%%ds" : : "rm" (sel));
459 }
460
461 static __inline void
462 load_es(u_int sel)
463 {
464         __asm __volatile("mov %0,%%es" : : "rm" (sel));
465 }
466
467 static __inline void
468 cpu_monitor(const void *addr, int extensions, int hints)
469 {
470         __asm __volatile("monitor;"
471             : :"a" (addr), "c" (extensions), "d"(hints));
472 }
473
474 static __inline void
475 cpu_mwait(int extensions, int hints)
476 {
477         __asm __volatile("mwait;" : :"a" (hints), "c" (extensions));
478 }
479
480 #ifdef _KERNEL
481 /* This is defined in <machine/specialreg.h> but is too painful to get to */
482 #ifndef MSR_FSBASE
483 #define MSR_FSBASE      0xc0000100
484 #endif
485 static __inline void
486 load_fs(u_int sel)
487 {
488         /* Preserve the fsbase value across the selector load */
489         __asm __volatile("rdmsr; mov %0,%%fs; wrmsr"
490             : : "rm" (sel), "c" (MSR_FSBASE) : "eax", "edx");
491 }
492
493 #ifndef MSR_GSBASE
494 #define MSR_GSBASE      0xc0000101
495 #endif
496 static __inline void
497 load_gs(u_int sel)
498 {
499         /*
500          * Preserve the gsbase value across the selector load.
501          * Note that we have to disable interrupts because the gsbase
502          * being trashed happens to be the kernel gsbase at the time.
503          */
504         __asm __volatile("pushfq; cli; rdmsr; mov %0,%%gs; wrmsr; popfq"
505             : : "rm" (sel), "c" (MSR_GSBASE) : "eax", "edx");
506 }
507 #else
508 /* Usable by userland */
509 static __inline void
510 load_fs(u_int sel)
511 {
512         __asm __volatile("mov %0,%%fs" : : "rm" (sel));
513 }
514
515 static __inline void
516 load_gs(u_int sel)
517 {
518         __asm __volatile("mov %0,%%gs" : : "rm" (sel));
519 }
520 #endif
521
522 static __inline void
523 lidt(struct region_descriptor *addr)
524 {
525         __asm __volatile("lidt (%0)" : : "r" (addr));
526 }
527
528 static __inline void
529 lldt(u_short sel)
530 {
531         __asm __volatile("lldt %0" : : "r" (sel));
532 }
533
534 static __inline void
535 ltr(u_short sel)
536 {
537         __asm __volatile("ltr %0" : : "r" (sel));
538 }
539
540 static __inline u_int64_t
541 rdr0(void)
542 {
543         u_int64_t data;
544         __asm __volatile("movq %%dr0,%0" : "=r" (data));
545         return (data);
546 }
547
548 static __inline void
549 load_dr0(u_int64_t dr0)
550 {
551         __asm __volatile("movq %0,%%dr0" : : "r" (dr0));
552 }
553
554 static __inline u_int64_t
555 rdr1(void)
556 {
557         u_int64_t data;
558         __asm __volatile("movq %%dr1,%0" : "=r" (data));
559         return (data);
560 }
561
562 static __inline void
563 load_dr1(u_int64_t dr1)
564 {
565         __asm __volatile("movq %0,%%dr1" : : "r" (dr1));
566 }
567
568 static __inline u_int64_t
569 rdr2(void)
570 {
571         u_int64_t data;
572         __asm __volatile("movq %%dr2,%0" : "=r" (data));
573         return (data);
574 }
575
576 static __inline void
577 load_dr2(u_int64_t dr2)
578 {
579         __asm __volatile("movq %0,%%dr2" : : "r" (dr2));
580 }
581
582 static __inline u_int64_t
583 rdr3(void)
584 {
585         u_int64_t data;
586         __asm __volatile("movq %%dr3,%0" : "=r" (data));
587         return (data);
588 }
589
590 static __inline void
591 load_dr3(u_int64_t dr3)
592 {
593         __asm __volatile("movq %0,%%dr3" : : "r" (dr3));
594 }
595
596 static __inline u_int64_t
597 rdr4(void)
598 {
599         u_int64_t data;
600         __asm __volatile("movq %%dr4,%0" : "=r" (data));
601         return (data);
602 }
603
604 static __inline void
605 load_dr4(u_int64_t dr4)
606 {
607         __asm __volatile("movq %0,%%dr4" : : "r" (dr4));
608 }
609
610 static __inline u_int64_t
611 rdr5(void)
612 {
613         u_int64_t data;
614         __asm __volatile("movq %%dr5,%0" : "=r" (data));
615         return (data);
616 }
617
618 static __inline void
619 load_dr5(u_int64_t dr5)
620 {
621         __asm __volatile("movq %0,%%dr5" : : "r" (dr5));
622 }
623
624 static __inline u_int64_t
625 rdr6(void)
626 {
627         u_int64_t data;
628         __asm __volatile("movq %%dr6,%0" : "=r" (data));
629         return (data);
630 }
631
632 static __inline void
633 load_dr6(u_int64_t dr6)
634 {
635         __asm __volatile("movq %0,%%dr6" : : "r" (dr6));
636 }
637
638 static __inline u_int64_t
639 rdr7(void)
640 {
641         u_int64_t data;
642         __asm __volatile("movq %%dr7,%0" : "=r" (data));
643         return (data);
644 }
645
646 static __inline void
647 load_dr7(u_int64_t dr7)
648 {
649         __asm __volatile("movq %0,%%dr7" : : "r" (dr7));
650 }
651
652 static __inline register_t
653 intr_disable(void)
654 {
655         register_t rflags;
656
657         rflags = read_rflags();
658         disable_intr();
659         return (rflags);
660 }
661
662 static __inline void
663 intr_restore(register_t rflags)
664 {
665         write_rflags(rflags);
666 }
667
668 #else /* !(__GNUCLIKE_ASM && __CC_SUPPORTS___INLINE) */
669
670 int     breakpoint(void);
671 u_int   bsfl(u_int mask);
672 u_int   bsrl(u_int mask);
673 void    disable_intr(void);
674 void    do_cpuid(u_int ax, u_int *p);
675 void    enable_intr(void);
676 void    halt(void);
677 void    ia32_pause(void);
678 u_char  inb(u_int port);
679 u_int   inl(u_int port);
680 void    insb(u_int port, void *addr, size_t cnt);
681 void    insl(u_int port, void *addr, size_t cnt);
682 void    insw(u_int port, void *addr, size_t cnt);
683 register_t      intr_disable(void);
684 void    intr_restore(register_t rf);
685 void    invd(void);
686 void    invlpg(u_int addr);
687 void    invltlb(void);
688 u_short inw(u_int port);
689 void    lidt(struct region_descriptor *addr);
690 void    lldt(u_short sel);
691 void    load_cr0(u_long cr0);
692 void    load_cr3(u_long cr3);
693 void    load_cr4(u_long cr4);
694 void    load_dr0(u_int64_t dr0);
695 void    load_dr1(u_int64_t dr1);
696 void    load_dr2(u_int64_t dr2);
697 void    load_dr3(u_int64_t dr3);
698 void    load_dr4(u_int64_t dr4);
699 void    load_dr5(u_int64_t dr5);
700 void    load_dr6(u_int64_t dr6);
701 void    load_dr7(u_int64_t dr7);
702 void    load_fs(u_int sel);
703 void    load_gs(u_int sel);
704 void    ltr(u_short sel);
705 void    outb(u_int port, u_char data);
706 void    outl(u_int port, u_int data);
707 void    outsb(u_int port, const void *addr, size_t cnt);
708 void    outsl(u_int port, const void *addr, size_t cnt);
709 void    outsw(u_int port, const void *addr, size_t cnt);
710 void    outw(u_int port, u_short data);
711 u_long  rcr0(void);
712 u_long  rcr2(void);
713 u_long  rcr3(void);
714 u_long  rcr4(void);
715 u_int64_t rdmsr(u_int msr);
716 u_int64_t rdpmc(u_int pmc);
717 u_int64_t rdr0(void);
718 u_int64_t rdr1(void);
719 u_int64_t rdr2(void);
720 u_int64_t rdr3(void);
721 u_int64_t rdr4(void);
722 u_int64_t rdr5(void);
723 u_int64_t rdr6(void);
724 u_int64_t rdr7(void);
725 u_int64_t rdtsc(void);
726 u_int   read_rflags(void);
727 u_int   rfs(void);
728 u_int   rgs(void);
729 void    wbinvd(void);
730 void    write_rflags(u_int rf);
731 void    wrmsr(u_int msr, u_int64_t newval);
732
733 #endif  /* __GNUCLIKE_ASM && __CC_SUPPORTS___INLINE */
734
735 void    reset_dbregs(void);
736
737 #ifdef _KERNEL
738 int     rdmsr_safe(u_int msr, uint64_t *val);
739 int     wrmsr_safe(u_int msr, uint64_t newval);
740 #endif
741
742 #endif /* !_MACHINE_CPUFUNC_H_ */