control_regs.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) 2012 The Chromium OS Authors.
  3. *
  4. * (C) Copyright 2008-2011
  5. * Graeme Russ, <graeme.russ@gmail.com>
  6. *
  7. * (C) Copyright 2002
  8. * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
  9. *
  10. * Portions of this file are derived from the Linux kernel source
  11. * Copyright (C) 1991, 1992 Linus Torvalds
  12. *
  13. * SPDX-License-Identifier: GPL-2.0+
  14. */
  15. #ifndef __X86_CONTROL_REGS_H
  16. #define __X86_CONTROL_REGS_H
  17. /*
  18. * The memory clobber prevents the GCC from reordering the read/write order
  19. * of CR0
  20. */
  21. static inline unsigned long read_cr0(void)
  22. {
  23. unsigned long val;
  24. asm volatile ("movl %%cr0, %0" : "=r" (val) : : "memory");
  25. return val;
  26. }
  27. static inline void write_cr0(unsigned long val)
  28. {
  29. asm volatile ("movl %0, %%cr0" : : "r" (val) : "memory");
  30. }
  31. static inline unsigned long read_cr2(void)
  32. {
  33. unsigned long val;
  34. asm volatile("mov %%cr2,%0\n\t" : "=r" (val) : : "memory");
  35. return val;
  36. }
  37. static inline unsigned long read_cr3(void)
  38. {
  39. unsigned long val;
  40. asm volatile("mov %%cr3,%0\n\t" : "=r" (val) : : "memory");
  41. return val;
  42. }
  43. static inline unsigned long read_cr4(void)
  44. {
  45. unsigned long val;
  46. asm volatile("mov %%cr4,%0\n\t" : "=r" (val) : : "memory");
  47. return val;
  48. }
  49. static inline unsigned long get_debugreg(int regno)
  50. {
  51. unsigned long val = 0; /* Damn you, gcc! */
  52. switch (regno) {
  53. case 0:
  54. asm("mov %%db0, %0" : "=r" (val));
  55. break;
  56. case 1:
  57. asm("mov %%db1, %0" : "=r" (val));
  58. break;
  59. case 2:
  60. asm("mov %%db2, %0" : "=r" (val));
  61. break;
  62. case 3:
  63. asm("mov %%db3, %0" : "=r" (val));
  64. break;
  65. case 6:
  66. asm("mov %%db6, %0" : "=r" (val));
  67. break;
  68. case 7:
  69. asm("mov %%db7, %0" : "=r" (val));
  70. break;
  71. default:
  72. val = 0;
  73. }
  74. return val;
  75. }
  76. #endif