segment.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-08-06 JasonHu first version
  9. */
  10. #ifndef __X86_SEGMENT_H__
  11. #define __X86_SEGMENT_H__
  12. #include <rtconfig.h>
  13. /* DA: Descriptor Attribute */
  14. #define DA_32 0x4000 /* 32 bits segment */
  15. #define DA_G 0x8000 /* segment limit is 4KB */
  16. #define DA_DPL0 0x00 /* DPL = 0 */
  17. #define DA_DPL1 0x20 /* DPL = 1 */
  18. #define DA_DPL2 0x40 /* DPL = 2 */
  19. #define DA_DPL3 0x60 /* DPL = 3 */
  20. #define DA_DR 0x90 /* readonly data */
  21. #define DA_DRW 0x92 /* read/write data */
  22. #define DA_DRWA 0x93 /* accessed read/write data */
  23. #define DA_C 0x98 /* code */
  24. #define DA_CR 0x9A /* readable code */
  25. #define DA_CCO 0x9C /* only execute consistent code segment */
  26. #define DA_CCOR 0x9E /* executable readable and consistent code segment */
  27. #define DA_LDT 0x82 /* local descriptor table */
  28. #define DA_386TSS 0x89 /* 386 TSS */
  29. /* SA : Selector Attribute */
  30. #define SA_RPL0 0
  31. #define SA_RPL1 1
  32. #define SA_RPL2 2
  33. #define SA_RPL3 3
  34. #define SA_TIG 0 /* selector in GDT */
  35. #define SA_TIL 1 /* selector in IDT */
  36. /* index of descriptor */
  37. #define INDEX_DUMMY 0
  38. #define INDEX_KERNEL_CODE 1
  39. #define INDEX_KERNEL_DATA 2
  40. #define INDEX_TSS 3
  41. #define INDEX_USER_CODE 4
  42. #define INDEX_USER_DATA 5
  43. #define INDEX_USER_TLS 6
  44. #define KERNEL_CODE_SEL ((INDEX_KERNEL_CODE << 3) + (SA_TIG << 2) + SA_RPL0)
  45. #define KERNEL_DATA_SEL ((INDEX_KERNEL_DATA << 3) + (SA_TIG << 2) + SA_RPL0)
  46. #define KERNEL_STACK_SEL KERNEL_DATA_SEL
  47. #define KERNEL_TSS_SEL ((INDEX_TSS << 3) + (SA_TIG << 2) + SA_RPL0)
  48. #define USER_CODE_SEL ((INDEX_USER_CODE << 3) + (SA_TIG << 2) + SA_RPL3)
  49. #define USER_DATA_SEL ((INDEX_USER_DATA << 3) + (SA_TIG << 2) + SA_RPL3)
  50. #define USER_STACK_SEL USER_DATA_SEL
  51. #define USER_TLS_SEL ((INDEX_USER_TLS << 3) + (SA_TIG << 2) + SA_RPL3)
  52. #define GDT_LIMIT 0x000007ff
  53. #define GDT_PADDR 0x003F0000
  54. #define GDT_VADDR (KERNEL_VADDR_START + GDT_PADDR)
  55. #define GDT_OFF2PTR(gdt, off) (gdt + off)
  56. #define GDT_BOUND_BOTTOM 0
  57. #define GDT_BOUND_TOP 0xffffffff
  58. #define GDT_KERNEL_CODE_ATTR (DA_CR | DA_DPL0 | DA_32 | DA_G)
  59. #define GDT_KERNEL_DATA_ATTR (DA_DRW | DA_DPL0 | DA_32 | DA_G)
  60. #define GDT_USER_CODE_ATTR (DA_CR | DA_DPL3 | DA_32 | DA_G)
  61. #define GDT_USER_DATA_ATTR (DA_DRW | DA_DPL3 | DA_32 | DA_G)
  62. #define GDT_TSS_ATTR (DA_386TSS)
  63. #define GDT_USER_TLS_ATTR (DA_DR | DA_DPL3 | DA_32 | DA_G) /* read only data seg */
  64. #ifndef __ASSEMBLY__
  65. #include <rtdef.h>
  66. void rt_hw_segment_init(void);
  67. void rt_hw_seg_tls_set(rt_ubase_t base);
  68. rt_ubase_t rt_hw_seg_tls_get();
  69. #endif
  70. #endif /*__X86_SEGMENT_H__*/