IOWindow.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/Bus/PCI/API.h>
  7. #include <Kernel/Bus/PCI/Definitions.h>
  8. #include <Kernel/IOWindow.h>
  9. namespace Kernel {
  10. #if ARCH(X86_64)
  11. ErrorOr<NonnullOwnPtr<IOWindow>> IOWindow::create_for_io_space(IOAddress address, u64 space_length)
  12. {
  13. VERIFY(!Checked<u64>::addition_would_overflow(address.get(), space_length));
  14. auto io_address_range = TRY(adopt_nonnull_own_or_enomem(new (nothrow) IOAddressData(address.get(), space_length)));
  15. return TRY(adopt_nonnull_own_or_enomem(new (nothrow) IOWindow(move(io_address_range))));
  16. }
  17. IOWindow::IOWindow(NonnullOwnPtr<IOAddressData> io_range)
  18. : m_space_type(SpaceType::IO)
  19. , m_io_range(move(io_range))
  20. {
  21. }
  22. #endif
  23. ErrorOr<NonnullOwnPtr<IOWindow>> IOWindow::create_from_io_window_with_offset(u64 offset, u64 space_length)
  24. {
  25. #if ARCH(X86_64)
  26. if (m_space_type == SpaceType::IO) {
  27. VERIFY(m_io_range);
  28. if (Checked<u64>::addition_would_overflow(m_io_range->address(), space_length))
  29. return Error::from_errno(EOVERFLOW);
  30. auto io_address_range = TRY(adopt_nonnull_own_or_enomem(new (nothrow) IOAddressData(as_io_address().offset(offset).get(), space_length)));
  31. return TRY(adopt_nonnull_own_or_enomem(new (nothrow) IOWindow(move(io_address_range))));
  32. }
  33. #endif
  34. VERIFY(space_type() == SpaceType::Memory);
  35. VERIFY(m_memory_mapped_range);
  36. if (Checked<u64>::addition_would_overflow(m_memory_mapped_range->paddr.get(), offset))
  37. return Error::from_errno(EOVERFLOW);
  38. if (Checked<u64>::addition_would_overflow(m_memory_mapped_range->paddr.get() + offset, space_length))
  39. return Error::from_errno(EOVERFLOW);
  40. auto memory_mapped_range = TRY(Memory::adopt_new_nonnull_own_typed_mapping<u8 volatile>(m_memory_mapped_range->paddr.offset(offset), space_length, Memory::Region::Access::ReadWrite));
  41. return TRY(adopt_nonnull_own_or_enomem(new (nothrow) IOWindow(move(memory_mapped_range))));
  42. }
  43. ErrorOr<NonnullOwnPtr<IOWindow>> IOWindow::create_from_io_window_with_offset(u64 offset)
  44. {
  45. #if ARCH(X86_64)
  46. if (m_space_type == SpaceType::IO) {
  47. VERIFY(m_io_range);
  48. VERIFY(m_io_range->space_length() >= offset);
  49. return create_from_io_window_with_offset(offset, m_io_range->space_length() - offset);
  50. }
  51. #endif
  52. VERIFY(space_type() == SpaceType::Memory);
  53. VERIFY(m_memory_mapped_range);
  54. VERIFY(m_memory_mapped_range->length >= offset);
  55. return create_from_io_window_with_offset(offset, m_memory_mapped_range->length - offset);
  56. }
  57. ErrorOr<NonnullOwnPtr<IOWindow>> IOWindow::create_for_pci_device_bar(PCI::DeviceIdentifier const& pci_device_identifier, PCI::HeaderType0BaseRegister pci_bar, u64 space_length)
  58. {
  59. u64 pci_bar_value = PCI::get_BAR(pci_device_identifier, pci_bar);
  60. auto pci_bar_space_type = PCI::get_BAR_space_type(pci_bar_value);
  61. if (pci_bar_space_type == PCI::BARSpaceType::Memory64BitSpace) {
  62. // FIXME: In theory, BAR5 cannot be assigned to 64 bit as it is the last one...
  63. // however, there might be 64 bit BAR5 for real bare metal hardware, so remove this
  64. // if it makes a problem.
  65. if (pci_bar == PCI::HeaderType0BaseRegister::BAR5) {
  66. return Error::from_errno(EINVAL);
  67. }
  68. u64 next_pci_bar_value = PCI::get_BAR(pci_device_identifier, static_cast<PCI::HeaderType0BaseRegister>(to_underlying(pci_bar) + 1));
  69. pci_bar_value |= next_pci_bar_value << 32;
  70. }
  71. auto pci_bar_space_size = PCI::get_BAR_space_size(pci_device_identifier, pci_bar);
  72. if (pci_bar_space_size < space_length)
  73. return Error::from_errno(EIO);
  74. if (pci_bar_space_type == PCI::BARSpaceType::IOSpace) {
  75. #if ARCH(X86_64)
  76. if (Checked<u64>::addition_would_overflow(pci_bar_value, space_length))
  77. return Error::from_errno(EOVERFLOW);
  78. auto io_address_range = TRY(adopt_nonnull_own_or_enomem(new (nothrow) IOAddressData((pci_bar_value & 0xfffffffc), space_length)));
  79. return TRY(adopt_nonnull_own_or_enomem(new (nothrow) IOWindow(move(io_address_range))));
  80. #else
  81. // Note: For non-x86 platforms, IO PCI BARs are simply not useable.
  82. return Error::from_errno(ENOTSUP);
  83. #endif
  84. }
  85. if (pci_bar_space_type == PCI::BARSpaceType::Memory32BitSpace && Checked<u32>::addition_would_overflow(pci_bar_value, space_length))
  86. return Error::from_errno(EOVERFLOW);
  87. if (pci_bar_space_type == PCI::BARSpaceType::Memory16BitSpace && Checked<u16>::addition_would_overflow(pci_bar_value, space_length))
  88. return Error::from_errno(EOVERFLOW);
  89. if (pci_bar_space_type == PCI::BARSpaceType::Memory64BitSpace && Checked<u64>::addition_would_overflow(pci_bar_value, space_length))
  90. return Error::from_errno(EOVERFLOW);
  91. auto memory_mapped_range = TRY(Memory::adopt_new_nonnull_own_typed_mapping<u8 volatile>(PhysicalAddress(pci_bar_value & 0xfffffff0), space_length, Memory::Region::Access::ReadWrite));
  92. return TRY(adopt_nonnull_own_or_enomem(new (nothrow) IOWindow(move(memory_mapped_range))));
  93. }
  94. ErrorOr<NonnullOwnPtr<IOWindow>> IOWindow::create_for_pci_device_bar(PCI::DeviceIdentifier const& pci_device_identifier, PCI::HeaderType0BaseRegister pci_bar)
  95. {
  96. u64 pci_bar_space_size = PCI::get_BAR_space_size(pci_device_identifier, pci_bar);
  97. return create_for_pci_device_bar(pci_device_identifier, pci_bar, pci_bar_space_size);
  98. }
  99. IOWindow::IOWindow(NonnullOwnPtr<Memory::TypedMapping<u8 volatile>> memory_mapped_range)
  100. : m_space_type(SpaceType::Memory)
  101. , m_memory_mapped_range(move(memory_mapped_range))
  102. {
  103. }
  104. IOWindow::~IOWindow() = default;
  105. bool IOWindow::is_access_aligned(u64 offset, size_t byte_size_access) const
  106. {
  107. return (offset % byte_size_access) == 0;
  108. }
  109. bool IOWindow::is_access_in_range(u64 offset, size_t byte_size_access) const
  110. {
  111. if (Checked<u64>::addition_would_overflow(offset, byte_size_access))
  112. return false;
  113. #if ARCH(X86_64)
  114. if (m_space_type == SpaceType::IO) {
  115. VERIFY(m_io_range);
  116. VERIFY(!Checked<u64>::addition_would_overflow(m_io_range->address(), m_io_range->space_length()));
  117. // To understand how we treat IO address space with the corresponding calculation, the Intel Software Developer manual
  118. // helps us to understand the layout of the IO address space -
  119. //
  120. // Intel® 64 and IA-32 Architectures Software Developer’s Manual, Volume 1: Basic Architecture, 16.3 I/O ADDRESS SPACE, page 16-1 wrote:
  121. // Any two consecutive 8-bit ports can be treated as a 16-bit port, and any four consecutive ports can be a 32-bit port.
  122. // In this manner, the processor can transfer 8, 16, or 32 bits to or from a device in the I/O address space.
  123. // Like words in memory, 16-bit ports should be aligned to even addresses (0, 2, 4, ...) so that all 16 bits can be transferred in a single bus cycle.
  124. // Likewise, 32-bit ports should be aligned to addresses that are multiples of four (0, 4, 8, ...).
  125. // The processor supports data transfers to unaligned ports, but there is a performance penalty because one or more
  126. // extra bus cycle must be used.
  127. return (m_io_range->address() + m_io_range->space_length()) >= (offset + byte_size_access);
  128. }
  129. #endif
  130. VERIFY(space_type() == SpaceType::Memory);
  131. VERIFY(m_memory_mapped_range);
  132. VERIFY(!Checked<u64>::addition_would_overflow(m_memory_mapped_range->offset, m_memory_mapped_range->length));
  133. return (m_memory_mapped_range->offset + m_memory_mapped_range->length) >= (offset + byte_size_access);
  134. }
  135. u8 IOWindow::read8(u64 offset)
  136. {
  137. VERIFY(is_access_in_range(offset, sizeof(u8)));
  138. u8 data { 0 };
  139. in<u8>(offset, data);
  140. return data;
  141. }
  142. u16 IOWindow::read16(u64 offset)
  143. {
  144. // Note: Although it might be OK to allow unaligned access on regular memory,
  145. // for memory mapped IO access, it should always be considered a bug.
  146. // The same goes for port mapped IO access, because in x86 unaligned access to ports
  147. // is possible but there's a performance penalty.
  148. VERIFY(is_access_in_range(offset, sizeof(u16)));
  149. VERIFY(is_access_aligned(offset, sizeof(u16)));
  150. u16 data { 0 };
  151. in<u16>(offset, data);
  152. return data;
  153. }
  154. u32 IOWindow::read32(u64 offset)
  155. {
  156. // Note: Although it might be OK to allow unaligned access on regular memory,
  157. // for memory mapped IO access, it should always be considered a bug.
  158. // The same goes for port mapped IO access, because in x86 unaligned access to ports
  159. // is possible but there's a performance penalty.
  160. VERIFY(is_access_in_range(offset, sizeof(u32)));
  161. VERIFY(is_access_aligned(offset, sizeof(u32)));
  162. u32 data { 0 };
  163. in<u32>(offset, data);
  164. return data;
  165. }
  166. void IOWindow::write8(u64 offset, u8 data)
  167. {
  168. VERIFY(is_access_in_range(offset, sizeof(u8)));
  169. out<u8>(offset, data);
  170. }
  171. void IOWindow::write16(u64 offset, u16 data)
  172. {
  173. // Note: Although it might be OK to allow unaligned access on regular memory,
  174. // for memory mapped IO access, it should always be considered a bug.
  175. // The same goes for port mapped IO access, because in x86 unaligned access to ports
  176. // is possible but there's a performance penalty.
  177. VERIFY(is_access_in_range(offset, sizeof(u16)));
  178. VERIFY(is_access_aligned(offset, sizeof(u16)));
  179. out<u16>(offset, data);
  180. }
  181. void IOWindow::write32(u64 offset, u32 data)
  182. {
  183. // Note: Although it might be OK to allow unaligned access on regular memory,
  184. // for memory mapped IO access, it should always be considered a bug.
  185. // The same goes for port mapped IO access, because in x86 unaligned access to ports
  186. // is possible but there's a performance penalty.
  187. VERIFY(is_access_in_range(offset, sizeof(u32)));
  188. VERIFY(is_access_aligned(offset, sizeof(u32)));
  189. out<u32>(offset, data);
  190. }
  191. void IOWindow::write32_unaligned(u64 offset, u32 data)
  192. {
  193. // Note: We only verify that we access IO in the expected range.
  194. // Note: for port mapped IO access, because in x86 unaligned access to ports
  195. // is possible but there's a performance penalty, we can still allow that to happen.
  196. // However, it should be noted that most cases should not use unaligned access
  197. // to hardware IO, so this is a valid case in emulators or hypervisors only.
  198. // Note: Using this for memory mapped IO will fail for unaligned access, because
  199. // there's no valid use case for it (yet).
  200. VERIFY(space_type() != SpaceType::Memory);
  201. VERIFY(is_access_in_range(offset, sizeof(u32)));
  202. out<u32>(offset, data);
  203. }
  204. u32 IOWindow::read32_unaligned(u64 offset)
  205. {
  206. // Note: We only verify that we access IO in the expected range.
  207. // Note: for port mapped IO access, because in x86 unaligned access to ports
  208. // is possible but there's a performance penalty, we can still allow that to happen.
  209. // However, it should be noted that most cases should not use unaligned access
  210. // to hardware IO, so this is a valid case in emulators or hypervisors only.
  211. // Note: Using this for memory mapped IO will fail for unaligned access, because
  212. // there's no valid use case for it (yet).
  213. VERIFY(space_type() != SpaceType::Memory);
  214. VERIFY(is_access_in_range(offset, sizeof(u32)));
  215. u32 data { 0 };
  216. in<u32>(offset, data);
  217. return data;
  218. }
  219. PhysicalAddress IOWindow::as_physical_memory_address() const
  220. {
  221. VERIFY(space_type() == SpaceType::Memory);
  222. VERIFY(m_memory_mapped_range);
  223. return m_memory_mapped_range->paddr;
  224. }
  225. u8 volatile* IOWindow::as_memory_address_pointer()
  226. {
  227. VERIFY(space_type() == SpaceType::Memory);
  228. VERIFY(m_memory_mapped_range);
  229. return m_memory_mapped_range->ptr();
  230. }
  231. #if ARCH(X86_64)
  232. IOAddress IOWindow::as_io_address() const
  233. {
  234. VERIFY(space_type() == SpaceType::IO);
  235. VERIFY(m_io_range);
  236. return IOAddress(m_io_range->address());
  237. }
  238. #endif
  239. }