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