hwvalid.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /******************************************************************************
  2. *
  3. * Module Name: hwvalid - I/O request validation
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #define _COMPONENT ACPI_HARDWARE
  45. ACPI_MODULE_NAME("hwvalid")
  46. /* Local prototypes */
  47. static acpi_status
  48. acpi_hw_validate_io_request(acpi_io_address address, u32 bit_width);
  49. /*
  50. * Protected I/O ports. Some ports are always illegal, and some are
  51. * conditionally illegal. This table must remain ordered by port address.
  52. *
  53. * The table is used to implement the Microsoft port access rules that
  54. * first appeared in Windows XP. Some ports are always illegal, and some
  55. * ports are only illegal if the BIOS calls _OSI with a win_XP string or
  56. * later (meaning that the BIOS itelf is post-XP.)
  57. *
  58. * This provides ACPICA with the desired port protections and
  59. * Microsoft compatibility.
  60. *
  61. * Description of port entries:
  62. * DMA: DMA controller
  63. * PIC0: Programmable Interrupt Controller (8259A)
  64. * PIT1: System Timer 1
  65. * PIT2: System Timer 2 failsafe
  66. * RTC: Real-time clock
  67. * CMOS: Extended CMOS
  68. * DMA1: DMA 1 page registers
  69. * DMA1L: DMA 1 Ch 0 low page
  70. * DMA2: DMA 2 page registers
  71. * DMA2L: DMA 2 low page refresh
  72. * ARBC: Arbitration control
  73. * SETUP: Reserved system board setup
  74. * POS: POS channel select
  75. * PIC1: Cascaded PIC
  76. * IDMA: ISA DMA
  77. * ELCR: PIC edge/level registers
  78. * PCI: PCI configuration space
  79. */
  80. static const struct acpi_port_info acpi_protected_ports[] = {
  81. {"DMA", 0x0000, 0x000F, ACPI_OSI_WIN_XP},
  82. {"PIC0", 0x0020, 0x0021, ACPI_ALWAYS_ILLEGAL},
  83. {"PIT1", 0x0040, 0x0043, ACPI_OSI_WIN_XP},
  84. {"PIT2", 0x0048, 0x004B, ACPI_OSI_WIN_XP},
  85. {"RTC", 0x0070, 0x0071, ACPI_OSI_WIN_XP},
  86. {"CMOS", 0x0074, 0x0076, ACPI_OSI_WIN_XP},
  87. {"DMA1", 0x0081, 0x0083, ACPI_OSI_WIN_XP},
  88. {"DMA1L", 0x0087, 0x0087, ACPI_OSI_WIN_XP},
  89. {"DMA2", 0x0089, 0x008B, ACPI_OSI_WIN_XP},
  90. {"DMA2L", 0x008F, 0x008F, ACPI_OSI_WIN_XP},
  91. {"ARBC", 0x0090, 0x0091, ACPI_OSI_WIN_XP},
  92. {"SETUP", 0x0093, 0x0094, ACPI_OSI_WIN_XP},
  93. {"POS", 0x0096, 0x0097, ACPI_OSI_WIN_XP},
  94. {"PIC1", 0x00A0, 0x00A1, ACPI_ALWAYS_ILLEGAL},
  95. {"IDMA", 0x00C0, 0x00DF, ACPI_OSI_WIN_XP},
  96. {"ELCR", 0x04D0, 0x04D1, ACPI_ALWAYS_ILLEGAL},
  97. {"PCI", 0x0CF8, 0x0CFF, ACPI_OSI_WIN_XP}
  98. };
  99. #define ACPI_PORT_INFO_ENTRIES ACPI_ARRAY_LENGTH (acpi_protected_ports)
  100. /******************************************************************************
  101. *
  102. * FUNCTION: acpi_hw_validate_io_request
  103. *
  104. * PARAMETERS: Address Address of I/O port/register
  105. * bit_width Number of bits (8,16,32)
  106. *
  107. * RETURN: Status
  108. *
  109. * DESCRIPTION: Validates an I/O request (address/length). Certain ports are
  110. * always illegal and some ports are only illegal depending on
  111. * the requests the BIOS AML code makes to the predefined
  112. * _OSI method.
  113. *
  114. ******************************************************************************/
  115. static acpi_status
  116. acpi_hw_validate_io_request(acpi_io_address address, u32 bit_width)
  117. {
  118. u32 i;
  119. u32 byte_width;
  120. acpi_io_address last_address;
  121. const struct acpi_port_info *port_info;
  122. ACPI_FUNCTION_TRACE(hw_validate_io_request);
  123. /* Supported widths are 8/16/32 */
  124. if ((bit_width != 8) && (bit_width != 16) && (bit_width != 32)) {
  125. ACPI_ERROR((AE_INFO,
  126. "Bad BitWidth parameter: %8.8X", bit_width));
  127. return (AE_BAD_PARAMETER);
  128. }
  129. port_info = acpi_protected_ports;
  130. byte_width = ACPI_DIV_8(bit_width);
  131. last_address = address + byte_width - 1;
  132. ACPI_DEBUG_PRINT((ACPI_DB_IO,
  133. "Address %8.8X%8.8X LastAddress %8.8X%8.8X Length %X",
  134. ACPI_FORMAT_UINT64(address),
  135. ACPI_FORMAT_UINT64(last_address), byte_width));
  136. /* Maximum 16-bit address in I/O space */
  137. if (last_address > ACPI_UINT16_MAX) {
  138. ACPI_ERROR((AE_INFO,
  139. "Illegal I/O port address/length above 64K: %8.8X%8.8X/0x%X",
  140. ACPI_FORMAT_UINT64(address), byte_width));
  141. return_ACPI_STATUS(AE_LIMIT);
  142. }
  143. /* Exit if requested address is not within the protected port table */
  144. if (address > acpi_protected_ports[ACPI_PORT_INFO_ENTRIES - 1].end) {
  145. return_ACPI_STATUS(AE_OK);
  146. }
  147. /* Check request against the list of protected I/O ports */
  148. for (i = 0; i < ACPI_PORT_INFO_ENTRIES; i++, port_info++) {
  149. /*
  150. * Check if the requested address range will write to a reserved
  151. * port. Four cases to consider:
  152. *
  153. * 1) Address range is contained completely in the port address range
  154. * 2) Address range overlaps port range at the port range start
  155. * 3) Address range overlaps port range at the port range end
  156. * 4) Address range completely encompasses the port range
  157. */
  158. if ((address <= port_info->end)
  159. && (last_address >= port_info->start)) {
  160. /* Port illegality may depend on the _OSI calls made by the BIOS */
  161. if (acpi_gbl_osi_data >= port_info->osi_dependency) {
  162. ACPI_DEBUG_PRINT((ACPI_DB_IO,
  163. "Denied AML access to port 0x%8.8X%8.8X/%X (%s 0x%.4X-0x%.4X)",
  164. ACPI_FORMAT_UINT64(address),
  165. byte_width, port_info->name,
  166. port_info->start,
  167. port_info->end));
  168. return_ACPI_STATUS(AE_AML_ILLEGAL_ADDRESS);
  169. }
  170. }
  171. /* Finished if address range ends before the end of this port */
  172. if (last_address <= port_info->end) {
  173. break;
  174. }
  175. }
  176. return_ACPI_STATUS(AE_OK);
  177. }
  178. /******************************************************************************
  179. *
  180. * FUNCTION: acpi_hw_read_port
  181. *
  182. * PARAMETERS: Address Address of I/O port/register to read
  183. * Value Where value is placed
  184. * Width Number of bits
  185. *
  186. * RETURN: Status and value read from port
  187. *
  188. * DESCRIPTION: Read data from an I/O port or register. This is a front-end
  189. * to acpi_os_read_port that performs validation on both the port
  190. * address and the length.
  191. *
  192. *****************************************************************************/
  193. acpi_status acpi_hw_read_port(acpi_io_address address, u32 *value, u32 width)
  194. {
  195. acpi_status status;
  196. u32 one_byte;
  197. u32 i;
  198. /* Truncate address to 16 bits if requested */
  199. if (acpi_gbl_truncate_io_addresses) {
  200. address &= ACPI_UINT16_MAX;
  201. }
  202. /* Validate the entire request and perform the I/O */
  203. status = acpi_hw_validate_io_request(address, width);
  204. if (ACPI_SUCCESS(status)) {
  205. status = acpi_os_read_port(address, value, width);
  206. return (status);
  207. }
  208. if (status != AE_AML_ILLEGAL_ADDRESS) {
  209. return (status);
  210. }
  211. /*
  212. * There has been a protection violation within the request. Fall
  213. * back to byte granularity port I/O and ignore the failing bytes.
  214. * This provides Windows compatibility.
  215. */
  216. for (i = 0, *value = 0; i < width; i += 8) {
  217. /* Validate and read one byte */
  218. if (acpi_hw_validate_io_request(address, 8) == AE_OK) {
  219. status = acpi_os_read_port(address, &one_byte, 8);
  220. if (ACPI_FAILURE(status)) {
  221. return (status);
  222. }
  223. *value |= (one_byte << i);
  224. }
  225. address++;
  226. }
  227. return (AE_OK);
  228. }
  229. /******************************************************************************
  230. *
  231. * FUNCTION: acpi_hw_write_port
  232. *
  233. * PARAMETERS: Address Address of I/O port/register to write
  234. * Value Value to write
  235. * Width Number of bits
  236. *
  237. * RETURN: Status
  238. *
  239. * DESCRIPTION: Write data to an I/O port or register. This is a front-end
  240. * to acpi_os_write_port that performs validation on both the port
  241. * address and the length.
  242. *
  243. *****************************************************************************/
  244. acpi_status acpi_hw_write_port(acpi_io_address address, u32 value, u32 width)
  245. {
  246. acpi_status status;
  247. u32 i;
  248. /* Truncate address to 16 bits if requested */
  249. if (acpi_gbl_truncate_io_addresses) {
  250. address &= ACPI_UINT16_MAX;
  251. }
  252. /* Validate the entire request and perform the I/O */
  253. status = acpi_hw_validate_io_request(address, width);
  254. if (ACPI_SUCCESS(status)) {
  255. status = acpi_os_write_port(address, value, width);
  256. return (status);
  257. }
  258. if (status != AE_AML_ILLEGAL_ADDRESS) {
  259. return (status);
  260. }
  261. /*
  262. * There has been a protection violation within the request. Fall
  263. * back to byte granularity port I/O and ignore the failing bytes.
  264. * This provides Windows compatibility.
  265. */
  266. for (i = 0; i < width; i += 8) {
  267. /* Validate and write one byte */
  268. if (acpi_hw_validate_io_request(address, 8) == AE_OK) {
  269. status =
  270. acpi_os_write_port(address, (value >> i) & 0xFF, 8);
  271. if (ACPI_FAILURE(status)) {
  272. return (status);
  273. }
  274. }
  275. address++;
  276. }
  277. return (AE_OK);
  278. }