control_regs.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. * See file CREDITS for list of people who contributed to this
  14. * project.
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation; either version 2 of
  19. * the License, or (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  29. * MA 02111-1307 USA
  30. */
  31. #ifndef __X86_CONTROL_REGS_H
  32. #define __X86_CONTROL_REGS_H
  33. /*
  34. * The memory clobber prevents the GCC from reordering the read/write order
  35. * of CR0
  36. */
  37. static inline unsigned long read_cr0(void)
  38. {
  39. unsigned long val;
  40. asm volatile ("movl %%cr0, %0" : "=r" (val) : : "memory");
  41. return val;
  42. }
  43. static inline void write_cr0(unsigned long val)
  44. {
  45. asm volatile ("movl %0, %%cr0" : : "r" (val) : "memory");
  46. }
  47. static inline unsigned long read_cr2(void)
  48. {
  49. unsigned long val;
  50. asm volatile("mov %%cr2,%0\n\t" : "=r" (val) : : "memory");
  51. return val;
  52. }
  53. static inline unsigned long read_cr3(void)
  54. {
  55. unsigned long val;
  56. asm volatile("mov %%cr3,%0\n\t" : "=r" (val) : : "memory");
  57. return val;
  58. }
  59. static inline unsigned long read_cr4(void)
  60. {
  61. unsigned long val;
  62. asm volatile("mov %%cr4,%0\n\t" : "=r" (val) : : "memory");
  63. return val;
  64. }
  65. static inline unsigned long get_debugreg(int regno)
  66. {
  67. unsigned long val = 0; /* Damn you, gcc! */
  68. switch (regno) {
  69. case 0:
  70. asm("mov %%db0, %0" : "=r" (val));
  71. break;
  72. case 1:
  73. asm("mov %%db1, %0" : "=r" (val));
  74. break;
  75. case 2:
  76. asm("mov %%db2, %0" : "=r" (val));
  77. break;
  78. case 3:
  79. asm("mov %%db3, %0" : "=r" (val));
  80. break;
  81. case 6:
  82. asm("mov %%db6, %0" : "=r" (val));
  83. break;
  84. case 7:
  85. asm("mov %%db7, %0" : "=r" (val));
  86. break;
  87. default:
  88. val = 0;
  89. }
  90. return val;
  91. }
  92. #endif