control_regs.h 1.7 KB

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