DescriptorTable.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Leon Albrecht <leon2002.la@gmail.com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #pragma once
  28. #include <AK/Types.h>
  29. #include <Kernel/VirtualAddress.h>
  30. #define GDT_SELECTOR_CODE0 0x08
  31. #define GDT_SELECTOR_DATA0 0x10
  32. #define GDT_SELECTOR_CODE3 0x18
  33. #define GDT_SELECTOR_DATA3 0x20
  34. #define GDT_SELECTOR_TLS 0x28
  35. #define GDT_SELECTOR_PROC 0x30
  36. #define GDT_SELECTOR_TSS 0x38
  37. // SYSENTER makes certain assumptions on how the GDT is structured:
  38. static_assert(GDT_SELECTOR_CODE0 + 8 == GDT_SELECTOR_DATA0); // SS0 = CS0 + 8
  39. // SYSEXIT makes certain assumptions on how the GDT is structured:
  40. static_assert(GDT_SELECTOR_CODE0 + 16 == GDT_SELECTOR_CODE3); // CS3 = CS0 + 16
  41. static_assert(GDT_SELECTOR_CODE0 + 24 == GDT_SELECTOR_DATA3); // SS3 = CS0 + 32
  42. namespace Kernel {
  43. struct [[gnu::packed]] DescriptorTablePointer {
  44. u16 limit;
  45. void* address;
  46. };
  47. union [[gnu::packed]] Descriptor {
  48. struct {
  49. u16 limit_lo;
  50. u16 base_lo;
  51. u8 base_hi;
  52. u8 type : 4;
  53. u8 descriptor_type : 1;
  54. u8 dpl : 2;
  55. u8 segment_present : 1;
  56. u8 limit_hi : 4;
  57. u8 : 1;
  58. u8 operation_size64 : 1;
  59. u8 operation_size32 : 1;
  60. u8 granularity : 1;
  61. u8 base_hi2;
  62. };
  63. struct {
  64. u32 low;
  65. u32 high;
  66. };
  67. enum Type {
  68. Invalid = 0,
  69. AvailableTSS_16bit = 0x1,
  70. LDT = 0x2,
  71. BusyTSS_16bit = 0x3,
  72. CallGate_16bit = 0x4,
  73. TaskGate = 0x5,
  74. InterruptGate_16bit = 0x6,
  75. TrapGate_16bit = 0x7,
  76. AvailableTSS_32bit = 0x9,
  77. BusyTSS_32bit = 0xb,
  78. CallGate_32bit = 0xc,
  79. InterruptGate_32bit = 0xe,
  80. TrapGate_32bit = 0xf,
  81. };
  82. VirtualAddress base() const
  83. {
  84. FlatPtr base = base_lo;
  85. base |= base_hi << 16u;
  86. base |= base_hi2 << 24u;
  87. return VirtualAddress { base };
  88. }
  89. void set_base(VirtualAddress base)
  90. {
  91. base_lo = base.get() & 0xffffu;
  92. base_hi = (base.get() >> 16u) & 0xffu;
  93. base_hi2 = (base.get() >> 24u) & 0xffu;
  94. }
  95. void set_limit(u32 length)
  96. {
  97. limit_lo = length & 0xffff;
  98. limit_hi = (length >> 16) & 0xf;
  99. }
  100. };
  101. }