mm_fault.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-12-06 WangXiaoyao the first version
  9. */
  10. #ifndef __MM_FAULT_H__
  11. #define __MM_FAULT_H__
  12. #include <rtthread.h>
  13. #include <stddef.h>
  14. #include <stdint.h>
  15. /* fast path fault handler, a page frame on kernel space is returned */
  16. #define MM_FAULT_STATUS_OK 0
  17. /* customized fault handler, done by using rt_varea_map_* */
  18. #define MM_FAULT_STATUS_OK_MAPPED 1
  19. #define MM_FAULT_STATUS_UNRECOVERABLE 4
  20. #define MM_FAULT_FIXABLE_FALSE 0
  21. #define MM_FAULT_FIXABLE_TRUE 1
  22. enum rt_mm_fault_op
  23. {
  24. MM_FAULT_OP_READ = 1,
  25. MM_FAULT_OP_WRITE,
  26. MM_FAULT_OP_EXECUTE,
  27. };
  28. enum rt_mm_fault_type
  29. {
  30. /**
  31. * Occurs when an instruction attempts to access a memory address that it
  32. * does not have R/W/X permission to access
  33. */
  34. MM_FAULT_TYPE_RWX_PERM,
  35. /* Without privileges to access (e.g. user accessing kernel) */
  36. MM_FAULT_TYPE_NO_PRIVILEGES,
  37. /**
  38. * Occurs when a load or store instruction accesses a virtual memory
  39. * address that is not currently mapped to a physical memory page
  40. */
  41. MM_FAULT_TYPE_PAGE_FAULT,
  42. /**
  43. * Occurs like a SIGBUS
  44. */
  45. MM_FAULT_TYPE_BUS_ERROR,
  46. /**
  47. * Occurs when page table walk failed, permission failed, writings on
  48. * non-dirty page.
  49. */
  50. MM_FAULT_TYPE_GENERIC_MMU,
  51. MM_FAULT_TYPE_GENERIC,
  52. __PRIVATE_PAGE_INSERT,
  53. };
  54. enum rt_mm_hint_prefetch
  55. {
  56. MM_FAULT_HINT_PREFETCH_NONE,
  57. MM_FAULT_HINT_PREFETCH_READY,
  58. };
  59. struct rt_mm_fault_res
  60. {
  61. void *vaddr;
  62. rt_size_t size;
  63. int status;
  64. /* hint for prefetch strategy */
  65. enum rt_mm_hint_prefetch hint;
  66. };
  67. struct rt_aspace_fault_msg
  68. {
  69. enum rt_mm_fault_op fault_op;
  70. enum rt_mm_fault_type fault_type;
  71. rt_size_t off;
  72. void *fault_vaddr;
  73. struct rt_mm_fault_res response;
  74. };
  75. struct rt_aspace_io_msg
  76. {
  77. /* offset in varea */
  78. rt_size_t off;
  79. /* fault address in target address space */
  80. void *fault_vaddr;
  81. /* read/write buffer in kernel space */
  82. void *buffer_vaddr;
  83. struct rt_mm_fault_res response;
  84. };
  85. rt_inline void rt_mm_fault_res_init(struct rt_mm_fault_res *res)
  86. {
  87. res->vaddr = RT_NULL;
  88. res->size = 0;
  89. res->hint = MM_FAULT_HINT_PREFETCH_NONE;
  90. res->status = MM_FAULT_STATUS_UNRECOVERABLE;
  91. }
  92. rt_inline void rt_mm_io_msg_init(struct rt_aspace_io_msg *io, rt_size_t off, void *fault_vaddr, void *buffer_vaddr)
  93. {
  94. io->off = off;
  95. io->fault_vaddr = fault_vaddr;
  96. io->buffer_vaddr = buffer_vaddr;
  97. rt_mm_fault_res_init(&io->response);
  98. }
  99. struct rt_aspace;
  100. /* MMU base page fault handler, MM_FAULT_FIXABLE_TRUE/MM_FAULT_FIXABLE_FALSE will be returned */
  101. int rt_aspace_fault_try_fix(struct rt_aspace *aspace, struct rt_aspace_fault_msg *msg);
  102. #endif /* __MM_FAULT_H__ */