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 --- Makefile | 19 ---- hwaccess_x86_io.c | 317 +++++++++++++++++++++++++++++++++++++++++++++++++++--- hwaccess_x86_io.h | 149 +++---------------------- meson.build | 18 ---- 4 files changed, 315 insertions(+), 188 deletions(-) diff --git a/Makefile b/Makefile index 53531a4aa..f5ed409e7 100644 --- a/Makefile +++ b/Makefile @@ -365,25 +365,6 @@ ifneq ($(ARCH), $(filter $(ARCH), x86 mips ppc arm sparc arc)) $(call mark_unsupported,$(DEPENDS_ON_RAW_MEM_ACCESS)) endif -ifeq ($(TARGET_OS), $(filter $(TARGET_OS), Linux Darwin NetBSD OpenBSD)) -FEATURE_FLAGS += -D'USE_IOPL=1' -else -FEATURE_FLAGS += -D'USE_IOPL=0' -endif - -ifeq ($(TARGET_OS), $(filter $(TARGET_OS), FreeBSD FreeBSD-glibc DragonFlyBSD)) -FEATURE_FLAGS += -D'USE_DEV_IO=1' -else -FEATURE_FLAGS += -D'USE_DEV_IO=0' -endif - -ifeq ($(TARGET_OS), $(filter $(TARGET_OS), Hurd)) -FEATURE_FLAGS += -D'USE_IOPERM=1' -else -FEATURE_FLAGS += -D'USE_IOPERM=0' -endif - - ############################################################################### # Flash chip drivers and bus support infrastructure. diff --git a/hwaccess_x86_io.c b/hwaccess_x86_io.c index e891d98d7..c3ad31396 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 diff --git a/hwaccess_x86_io.h b/hwaccess_x86_io.h index 0d16fdde4..31d1e86c8 100644 --- a/hwaccess_x86_io.h +++ b/hwaccess_x86_io.h @@ -2,6 +2,8 @@ * This file is part of the flashrom project. * * Copyright (C) 2009 Carl-Daniel Hailfinger + * Copyright (C) 2022 secunet Security Networks AG + * (Written by Thomas Heijligen -#elif defined(__linux__) || defined(__GLIBC__) -#include -#endif - -/* for iopl and outb under Solaris */ -#if defined (__sun) -#include -#include -#include -#endif - -int rget_io_perms(void); - -/* Clarification about OUTB/OUTW/OUTL argument order: - * OUT[BWL](val, port) +#include +/** */ +int rget_io_perms(void); -#if defined(__FreeBSD__) || defined(__DragonFly__) - /* Note that Debian/kFreeBSD (FreeBSD kernel with glibc) has conflicting - * out[bwl] definitions in machine/cpufunc.h and sys/io.h at least in some - * versions. Use machine/cpufunc.h only for plain FreeBSD/DragonFlyBSD. - */ - #include - #include - #define OUTB(x, y) do { u_int outb_tmp = (y); outb(outb_tmp, (x)); } while (0) - #define OUTW(x, y) do { u_int outw_tmp = (y); outw(outw_tmp, (x)); } while (0) - #define OUTL(x, y) do { u_int outl_tmp = (y); outl(outl_tmp, (x)); } while (0) - #define INB(x) __extension__ ({ u_int inb_tmp = (x); inb(inb_tmp); }) - #define INW(x) __extension__ ({ u_int inw_tmp = (x); inw(inw_tmp); }) - #define INL(x) __extension__ ({ u_int inl_tmp = (x); inl(inl_tmp); }) -#else - -#if defined (__sun) - /* Note different order for outb */ - #define OUTB(x,y) outb(y, x) - #define OUTW(x,y) outw(y, x) - #define OUTL(x,y) outl(y, x) - #define INB inb - #define INW inw - #define INL inl -#else - -#ifdef __DJGPP__ - -#include - - #define OUTB(x,y) outportb(y, x) - #define OUTW(x,y) outportw(y, x) - #define OUTL(x,y) outportl(y, x) - - #define INB inportb - #define INW inportw - #define INL inportl - -#else - -#if defined(__MACH__) && defined(__APPLE__) - /* Header is part of the DirectHW library. */ - #include -#endif - - /* This is the usual glibc interface. */ - #define OUTB outb - #define OUTW outw - #define OUTL outl - #define INB inb - #define INW inw - #define INL inl -#endif -#endif -#endif - -#if defined(__NetBSD__) || defined (__OpenBSD__) - #if defined(__i386__) || defined(__x86_64__) - #include - #include - #if defined(__NetBSD__) - #if defined(__i386__) - #define iopl i386_iopl - #elif defined(__x86_64__) - #define iopl x86_64_iopl - #endif - #elif defined (__OpenBSD__) - #if defined(__i386__) - #define iopl i386_iopl - #elif defined(__amd64__) - #define iopl amd64_iopl - #endif - #endif - -static inline void outb(uint8_t value, uint16_t port) -{ - __asm__ volatile ("outb %b0,%w1": :"a" (value), "Nd" (port)); -} - -static inline uint8_t inb(uint16_t port) -{ - uint8_t value; - __asm__ volatile ("inb %w1,%0":"=a" (value):"Nd" (port)); - return value; -} - -static inline void outw(uint16_t value, uint16_t port) -{ - __asm__ volatile ("outw %w0,%w1": :"a" (value), "Nd" (port)); -} - -static inline uint16_t inw(uint16_t port) -{ - uint16_t value; - __asm__ volatile ("inw %w1,%0":"=a" (value):"Nd" (port)); - return value; -} - -static inline void outl(uint32_t value, uint16_t port) -{ - __asm__ volatile ("outl %0,%w1": :"a" (value), "Nd" (port)); -} - -static inline uint32_t inl(uint16_t port) -{ - uint32_t value; - __asm__ volatile ("inl %1,%0":"=a" (value):"Nd" (port)); - return value; -} - #endif -#endif +void OUTB(uint8_t value, uint16_t port); +void OUTW(uint16_t value, uint16_t port); +void OUTL(uint32_t value, uint16_t port); +uint8_t INB(uint16_t port); +uint16_t INW(uint16_t port); +uint32_t INL(uint16_t port); #endif /* __HWACCESS_X86_IO_H__ */ diff --git a/meson.build b/meson.build index cc393a7c1..b7fc25fb7 100644 --- a/meson.build +++ b/meson.build @@ -42,24 +42,6 @@ else add_project_arguments('-DIS_WINDOWS=0', language : 'c') endif -if host_machine.system() in ['linux', 'darwin', 'netbsd', 'openbsd'] - add_project_arguments('-DUSE_IOPL=1', language : 'c') -else - add_project_arguments('-DUSE_IOPL=0', language : 'c') -endif - -if host_machine.system() in ['freebsd', 'dragonfly'] - add_project_arguments('-DUSE_DEV_IO=1', language : 'c') -else - add_project_arguments('-DUSE_DEV_IO=0', language : 'c') -endif - -if host_machine.system() in ['gnu'] - add_project_arguments('-DUSE_IOPERM=1', language : 'c') -else - add_project_arguments('-DUSE_IOPERM=0', language : 'c') -endif - # get defaults from configure config_atahpt = get_option('config_atahpt') config_atapromise = get_option('config_atapromise') -- cgit v1.2.3