cacheflush.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #ifndef _ASM_X86_CACHEFLUSH_H
  2. #define _ASM_X86_CACHEFLUSH_H
  3. /* Caches aren't brain-dead on the intel. */
  4. #include <asm-generic/cacheflush.h>
  5. #ifdef CONFIG_X86_PAT
  6. /*
  7. * X86 PAT uses page flags WC and Uncached together to keep track of
  8. * memory type of pages that have backing page struct. X86 PAT supports 3
  9. * different memory types, _PAGE_CACHE_WB, _PAGE_CACHE_WC and
  10. * _PAGE_CACHE_UC_MINUS and fourth state where page's memory type has not
  11. * been changed from its default (value of -1 used to denote this).
  12. * Note we do not support _PAGE_CACHE_UC here.
  13. */
  14. #define _PGMT_DEFAULT 0
  15. #define _PGMT_WC (1UL << PG_arch_1)
  16. #define _PGMT_UC_MINUS (1UL << PG_uncached)
  17. #define _PGMT_WB (1UL << PG_uncached | 1UL << PG_arch_1)
  18. #define _PGMT_MASK (1UL << PG_uncached | 1UL << PG_arch_1)
  19. #define _PGMT_CLEAR_MASK (~_PGMT_MASK)
  20. static inline unsigned long get_page_memtype(struct page *pg)
  21. {
  22. unsigned long pg_flags = pg->flags & _PGMT_MASK;
  23. if (pg_flags == _PGMT_DEFAULT)
  24. return -1;
  25. else if (pg_flags == _PGMT_WC)
  26. return _PAGE_CACHE_WC;
  27. else if (pg_flags == _PGMT_UC_MINUS)
  28. return _PAGE_CACHE_UC_MINUS;
  29. else
  30. return _PAGE_CACHE_WB;
  31. }
  32. static inline void set_page_memtype(struct page *pg, unsigned long memtype)
  33. {
  34. unsigned long memtype_flags = _PGMT_DEFAULT;
  35. unsigned long old_flags;
  36. unsigned long new_flags;
  37. switch (memtype) {
  38. case _PAGE_CACHE_WC:
  39. memtype_flags = _PGMT_WC;
  40. break;
  41. case _PAGE_CACHE_UC_MINUS:
  42. memtype_flags = _PGMT_UC_MINUS;
  43. break;
  44. case _PAGE_CACHE_WB:
  45. memtype_flags = _PGMT_WB;
  46. break;
  47. }
  48. do {
  49. old_flags = pg->flags;
  50. new_flags = (old_flags & _PGMT_CLEAR_MASK) | memtype_flags;
  51. } while (cmpxchg(&pg->flags, old_flags, new_flags) != old_flags);
  52. }
  53. #else
  54. static inline unsigned long get_page_memtype(struct page *pg) { return -1; }
  55. static inline void set_page_memtype(struct page *pg, unsigned long memtype) { }
  56. #endif
  57. /*
  58. * The set_memory_* API can be used to change various attributes of a virtual
  59. * address range. The attributes include:
  60. * Cachability : UnCached, WriteCombining, WriteBack
  61. * Executability : eXeutable, NoteXecutable
  62. * Read/Write : ReadOnly, ReadWrite
  63. * Presence : NotPresent
  64. *
  65. * Within a category, the attributes are mutually exclusive.
  66. *
  67. * The implementation of this API will take care of various aspects that
  68. * are associated with changing such attributes, such as:
  69. * - Flushing TLBs
  70. * - Flushing CPU caches
  71. * - Making sure aliases of the memory behind the mapping don't violate
  72. * coherency rules as defined by the CPU in the system.
  73. *
  74. * What this API does not do:
  75. * - Provide exclusion between various callers - including callers that
  76. * operation on other mappings of the same physical page
  77. * - Restore default attributes when a page is freed
  78. * - Guarantee that mappings other than the requested one are
  79. * in any state, other than that these do not violate rules for
  80. * the CPU you have. Do not depend on any effects on other mappings,
  81. * CPUs other than the one you have may have more relaxed rules.
  82. * The caller is required to take care of these.
  83. */
  84. int _set_memory_uc(unsigned long addr, int numpages);
  85. int _set_memory_wc(unsigned long addr, int numpages);
  86. int _set_memory_wb(unsigned long addr, int numpages);
  87. int set_memory_uc(unsigned long addr, int numpages);
  88. int set_memory_wc(unsigned long addr, int numpages);
  89. int set_memory_wb(unsigned long addr, int numpages);
  90. int set_memory_x(unsigned long addr, int numpages);
  91. int set_memory_nx(unsigned long addr, int numpages);
  92. int set_memory_ro(unsigned long addr, int numpages);
  93. int set_memory_rw(unsigned long addr, int numpages);
  94. int set_memory_np(unsigned long addr, int numpages);
  95. int set_memory_4k(unsigned long addr, int numpages);
  96. int set_memory_array_uc(unsigned long *addr, int addrinarray);
  97. int set_memory_array_wc(unsigned long *addr, int addrinarray);
  98. int set_memory_array_wb(unsigned long *addr, int addrinarray);
  99. int set_pages_array_uc(struct page **pages, int addrinarray);
  100. int set_pages_array_wc(struct page **pages, int addrinarray);
  101. int set_pages_array_wb(struct page **pages, int addrinarray);
  102. /*
  103. * For legacy compatibility with the old APIs, a few functions
  104. * are provided that work on a "struct page".
  105. * These functions operate ONLY on the 1:1 kernel mapping of the
  106. * memory that the struct page represents, and internally just
  107. * call the set_memory_* function. See the description of the
  108. * set_memory_* function for more details on conventions.
  109. *
  110. * These APIs should be considered *deprecated* and are likely going to
  111. * be removed in the future.
  112. * The reason for this is the implicit operation on the 1:1 mapping only,
  113. * making this not a generally useful API.
  114. *
  115. * Specifically, many users of the old APIs had a virtual address,
  116. * called virt_to_page() or vmalloc_to_page() on that address to
  117. * get a struct page* that the old API required.
  118. * To convert these cases, use set_memory_*() on the original
  119. * virtual address, do not use these functions.
  120. */
  121. int set_pages_uc(struct page *page, int numpages);
  122. int set_pages_wb(struct page *page, int numpages);
  123. int set_pages_x(struct page *page, int numpages);
  124. int set_pages_nx(struct page *page, int numpages);
  125. int set_pages_ro(struct page *page, int numpages);
  126. int set_pages_rw(struct page *page, int numpages);
  127. void clflush_cache_range(void *addr, unsigned int size);
  128. #ifdef CONFIG_DEBUG_RODATA
  129. void mark_rodata_ro(void);
  130. extern const int rodata_test_data;
  131. extern int kernel_set_to_readonly;
  132. void set_kernel_text_rw(void);
  133. void set_kernel_text_ro(void);
  134. #else
  135. static inline void set_kernel_text_rw(void) { }
  136. static inline void set_kernel_text_ro(void) { }
  137. #endif
  138. #ifdef CONFIG_DEBUG_RODATA_TEST
  139. int rodata_test(void);
  140. #else
  141. static inline int rodata_test(void)
  142. {
  143. return 0;
  144. }
  145. #endif
  146. #endif /* _ASM_X86_CACHEFLUSH_H */