From 0f51f62c6265af5977fec7425596b65a3926c42b Mon Sep 17 00:00:00 2001 From: Thomas Heijligen Date: Thu, 20 Jan 2022 16:45:14 +0100 Subject: hwaccess_x86_io: clean header concept Move all function implementations into the .c file TEST: `[g]make [WARNERROR=no]` on Linux, FreeBSD, NetBSD, OpenBSD, DragonflyBSD, OpenIndiana, Debian-GNU/Hurd Change-Id: I1400704e9ac5fed00c096796536108d5bfb875e3 Signed-off-by: Thomas Heijligen Reviewed-on: https://review.coreboot.org/c/flashrom/+/61276 Tested-by: build bot (Jenkins) Reviewed-by: Nico Huber --- hwaccess_x86_io.c | 317 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 300 insertions(+), 17 deletions(-) (limited to 'hwaccess_x86_io.c') diff --git a/hwaccess_x86_io.c b/hwaccess_x86_io.c index e891d98d..c3ad3139 100644 --- a/hwaccess_x86_io.c +++ b/hwaccess_x86_io.c @@ -17,12 +17,25 @@ */ /* - * This file implements x86 I/O port permission handling. + * This file implements x86 I/O port permission and access handling. * - * For this purpose the platform specific code is encapsuled in two functions - * and compiled only on the respective platform. `platform_get_io_perms()` is - * called to get the permissions and `platform_release_io_perms()` is called for - * releasing those. + * The first part of the file defines the platform dependend implementations to + * use on each platform. For getting I/O permissions set IO_PORT_PERMISSION to + * one of: + * - USE_DUMMY + * - USE_SUN + * - USE_DEVICE + * - USE_IOPERM + * - USE_IOPL + * For the IN[B/W/L] and OUT[B/W/L] functions set IO_PORT_FUNCTION to one of: + * - USE_LIBC_TARGET_LAST + * - USE_LIBC_TARGET_FIRST + * - USE_DOS + * - USE_ASM + * + * The platform specific code for getting I/O permissions consists of two + * functions. `platform_get_io_perms()` is called to get + * permissions and `platform_release_io_perms()` is called for releasing those. */ #include @@ -31,7 +44,126 @@ #include "flash.h" #include "hwaccess_x86_io.h" -#if defined(__DJGPP__) || defined(__LIBPAYLOAD) /* DJGPP, libpayload */ +/* IO_PORT_FUNCTION */ +#define USE_LIBC_TARGET_LAST 1 +#define USE_LIBC_TARGET_FIRST 2 +#define USE_ASM 3 +#define USE_DOS 4 + +/* IO_PORT_PERMISSION */ +#define USE_IOPL 5 +#define USE_DEVICE 6 +#define USE_DUMMY 7 +#define USE_SUN 8 +#define USE_IOPERM 9 + +#if defined(__ANDROID__) +#include + +#define IO_PORT_PERMISSION USE_IOPL +#define IO_PORT_FUNCTION USE_LIBC_TARGET_LAST +#endif + +#if defined(__linux__) && !defined(__ANDROID__) +#include + +#define IO_PORT_PERMISSION USE_IOPL +#define IO_PORT_FUNCTION USE_LIBC_TARGET_LAST +#endif + +#if defined(__FreeBSD_kernel) && defined(__GLIBC__) +#include +#include +#include +#include + +#define IO_PORT_PERMISSION USE_DEVICE +#define IO_PORT_FUNCTION USE_LIBC_TARGET_LAST +#endif + +#if defined(__FreeBSD__) || defined(__DragonFly__) +#include +#include +#include +#include + +#define IO_PORT_PERMISSION USE_DEVICE +#define IO_PORT_FUNCTION USE_LIBC_TARGET_FIRST +#endif + +#if defined(__NetBSD__) +#include +#include + +#if defined(__i386__) +#define iopl i386_iopl +#elif defined(__x86_64__) +#define iopl x86_64_iopl +#endif + +#define IO_PORT_PERMISSION USE_IOPL +#define IO_PORT_FUNCTION USE_ASM +#endif + +#if defined(__OpenBSD__) +#include +#include + +#if defined(__i386__) +#define iopl i386_iopl +#elif defined(__amd64__) +#define iopl amd64_iopl +#endif + +#define IO_PORT_PERMISSION USE_IOPL +#define IO_PORT_FUNCTION USE_ASM +#endif + +#if defined(__MACH__) && defined(__APPLE__) +#include + +#define IO_PORT_PERMISSION USE_IOPL +#define IO_PORT_FUNCTION USE_LIBC_TARGET_LAST +#endif + +#if defined(__DJGPP__) +#include + +#define IO_PORT_PERMISSION USE_DUMMY +#define IO_PORT_FUNCTION USE_DOS +#endif + +#if defined(__LIBPAYLOAD__) +#define IO_PORT_PERMISSION USE_DUMMY +#define IO_PORT_FUNCTION USE_LIBC_TARGET_LAST +#endif + +#if defined(__sun) +#include +#include +#include + +#define IO_PORT_PERMISSION USE_SUN +#define IO_PORT_FUNCTION USE_LIBC_TARGET_FIRST +#endif + +#if defined(__gnu_hurd__) +#include + +#define IO_PORT_PERMISSION USE_IOPERM +#define IO_PORT_FUNCTION USE_LIBC_TARGET_LAST +#endif + +/* Make sure IO_PORT_PERMISSION and IO_PORT_FUNCTION are set */ +#if IO_PORT_PERMISSION == 0 || IO_PORT_FUNCTION == 0 +#error Unsupported or misconfigured platform. +#endif + +/* + * USE_DUMMY + * This is for platforms which have no privilege levels. + */ +#if IO_PORT_PERMISSION == USE_DUMMY static int platform_get_io_perms(void) { return 0; @@ -43,9 +175,11 @@ static int platform_release_io_perms(void *p) } #endif -#if defined(__sun) /* SunOS */ -#include - +/* + * USE_SUN + * This implementation uses SunOS system call sysi86 to handle I/O port permissions. + */ +#if IO_PORT_PERMISSION == USE_SUN static int platform_get_io_perms(void) { return sysi86(SI86V86, V86SC_IOPL, PS_IOPL); @@ -58,10 +192,11 @@ static int platform_release_io_perms(void *p) } #endif -#if USE_DEV_IO /* FreeBSD, DragonFlyBSD */ -#include -#include - +/* + * USE_DEVICE + * This implementation uses the virtual /dev/io file to handle I/O Port permissions. + */ +#if IO_PORT_PERMISSION == USE_DEVICE int io_fd; static int platform_get_io_perms(void) @@ -77,9 +212,11 @@ static int platform_release_io_perms(void *p) } #endif -#if USE_IOPERM /* GNU Hurd */ -#include - +/* + * USE_IOPERM + * This implementation uses the ioperm system call to handle I/O port permissions. + */ +#if IO_PORT_PERMISSION == USE_IOPERM static int platform_get_io_perms(void) { return ioperm(0, 65536, 1); @@ -92,7 +229,11 @@ static int platform_release_io_perms(void *p) } #endif -#if USE_IOPL /* Linux, Darwin, NetBSD, OpenBSD */ +/* + * USE_IOPL + * This implementation uses the iopl system call to handle I/O port permissions. + */ +#if IO_PORT_PERMISSION == USE_IOPL static int platform_get_io_perms(void) { return iopl(3); @@ -129,3 +270,145 @@ int rget_io_perms(void) "that your kernel configuration has the option INSECURE enabled.\n"); return 1; } + +/* + * USE_LIBC_LAST + * This implementation wraps the glibc functions with the port as 2nd argument. + */ +#if IO_PORT_FUNCTION == USE_LIBC_TARGET_LAST +void OUTB(uint8_t value, uint16_t port) +{ + outb(value, port); +} + +void OUTW(uint16_t value, uint16_t port) +{ + outw(value, port); +} + +void OUTL(uint32_t value, uint16_t port) +{ + outl(value, port); +} +#endif + +/* + * USE_LIBC_FIRST + * This implementation uses libc based functions with the port as first argument + * like on BSDs. + */ +#if IO_PORT_FUNCTION == USE_LIBC_TARGET_FIRST +void OUTB(uint8_t value, uint16_t port) +{ + outb(port, value); +} + +void OUTW(uint16_t value, uint16_t port) +{ + outw(port, value); +} + +void OUTL(uint32_t value, uint16_t port) +{ + outl(port, value); +} +#endif + +/* + * LIBC_xxx + * INx functions can be shared between _LAST and _FIRST + */ +#if IO_PORT_FUNCTION == USE_LIBC_TARGET_LAST || IO_PORT_FUNCTION == USE_LIBC_TARGET_FIRST +uint8_t INB(uint16_t port) +{ + return inb(port); +} + +uint16_t INW(uint16_t port) +{ + return inw(port); +} + +uint32_t INL(uint16_t port) +{ + return inl(port); +} +#endif + +/* + * USE_DOS + * DOS provides the functions under a differnt name. + */ +#if IO_PORT_FUNCTION == USE_DOS +void OUTB(uint8_t value, uint16_t port) +{ + outportb(port, value); +} + +void OUTW(uint16_t value, uint16_t port) +{ + outportw(port, value); +} + +void OUTL(uint32_t value, uint16_t port) +{ + outportl(port, value); +} + +uint8_t INB(uint16_t port) +{ + return inportb(port); +} + +uint16_t INW(uint16_t port) +{ + return inportw(port); +} + +uint32_t INL(uint16_t port) +{ + return inportl(port); +} +#endif + +/* + * USE_ASM + * Pure assembly implementation. + */ +#if IO_PORT_FUNCTION == USE_ASM +void OUTB(uint8_t value, uint16_t port) +{ + __asm__ volatile ("outb %b0,%w1": :"a" (value), "Nd" (port)); +} + +void OUTW(uint16_t value, uint16_t port) +{ + __asm__ volatile ("outw %w0,%w1": :"a" (value), "Nd" (port)); +} + +void OUTL(uint32_t value, uint16_t port) +{ + __asm__ volatile ("outl %0,%w1": :"a" (value), "Nd" (port)); +} + +uint8_t INB(uint16_t port) +{ + uint8_t value; + __asm__ volatile ("inb %w1,%0":"=a" (value):"Nd" (port)); + return value; +} + +uint16_t INW(uint16_t port) +{ + uint16_t value; + __asm__ volatile ("inw %w1,%0":"=a" (value):"Nd" (port)); + return value; +} + +uint32_t INL(uint16_t port) +{ + uint32_t value; + __asm__ volatile ("inl %1,%0":"=a" (value):"Nd" (port)); + return value; +} +#endif -- cgit v1.2.3