"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/stress-ioport.c" (15 Mar 2019, 4113 Bytes) of package /linux/privat/stress-ng-0.09.56.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "stress-ioport.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.09.49_vs_0.09.50.

    1 /*
    2  * Copyright (C) 2013-2019 Canonical, Ltd.
    3  *
    4  * This program is free software; you can redistribute it and/or
    5  * modify it under the terms of the GNU General Public License
    6  * as published by the Free Software Foundation; either version 2
    7  * of the License, or (at your option) any later version.
    8  *
    9  * This program is distributed in the hope that it will be useful,
   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  * GNU General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU General Public License
   15  * along with this program; if not, write to the Free Software
   16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
   17  *
   18  * This code is a complete clean re-write of the stress tool by
   19  * Colin Ian King <colin.king@canonical.com> and attempts to be
   20  * backwardly compatible with the stress tool by Amos Waterland
   21  * <apw@rossby.metr.ou.edu> but has more stress tests and more
   22  * functionality.
   23  *
   24  */
   25 #include "stress-ng.h"
   26 
   27 #define IO_PORT     0x80
   28 
   29 #define IOPORT_OPT_IN   0x00000001
   30 #define IOPORT_OPT_OUT  0x00000002
   31 
   32 typedef struct {
   33     const char  *opt;
   34     const uint32_t  flag;
   35 } ioport_opts_t;
   36 
   37 static const ioport_opts_t ioport_opts[] = {
   38     { "in",     IOPORT_OPT_IN },
   39     { "out",    IOPORT_OPT_OUT },
   40     { "inout",  IOPORT_OPT_IN | IOPORT_OPT_OUT },
   41 };
   42 
   43 int stress_set_ioport_opts(const char *opts)
   44 {
   45     size_t i;
   46 
   47     for (i = 0; i < SIZEOF_ARRAY(ioport_opts); i++) {
   48         if (!strcmp(opts, ioport_opts[i].opt)) {
   49             uint32_t flag = ioport_opts[i].flag;
   50 
   51             set_setting("ioport-opts", TYPE_ID_UINT32, &flag);
   52             return 0;
   53         }
   54     }
   55 
   56     (void)fprintf(stderr, "ioport-opt option '%s' not known, options are:", opts);
   57     for (i = 0; i < SIZEOF_ARRAY(ioport_opts); i++) {
   58         (void)fprintf(stderr, "%s %s",
   59             i == 0 ? "" : ",", ioport_opts[i].opt);
   60     }
   61     (void)fprintf(stderr, "\n");
   62     return -1;
   63 }
   64 
   65 #if defined(STRESS_X86) &&  \
   66     defined(HAVE_IOPORT) && \
   67     defined(HAVE_SYS_IO_H)
   68 
   69 static int stress_ioport_supported(void)
   70 {
   71     int ret;
   72 
   73     ret = ioperm(IO_PORT, 1, 1);
   74     if (ret < 0) {
   75         switch (errno) {
   76         case ENOMEM:
   77             pr_inf("ioport: out of memory, skipping stressor\n");
   78             return -1;
   79         case EPERM:
   80             pr_inf("ioport: insufficient privilege, invoke with CAP_SYS_RAWIO privilege, skipping stressor\n");
   81             return -1;
   82         case EINVAL:
   83         case EIO:
   84         default:
   85             pr_inf("ioport: cannot access port 0x%x, not skipping stressor\n", IO_PORT);
   86             return -1;
   87         }
   88     }
   89     (void)ioperm(IO_PORT, 1, 0);
   90     return 0;
   91 }
   92 
   93 /*
   94  *  stress_ioport()
   95  *  stress performs I/O port I/O transactions
   96  */
   97 static int stress_ioport(const args_t *args)
   98 {
   99     int ret;
  100     uint32_t flag = 0;
  101     unsigned char v;
  102 
  103     (void)get_setting("ioport-opts", &flag);
  104     if (!flag)
  105         flag = IOPORT_OPT_IN | IOPORT_OPT_OUT;
  106 
  107     ret = ioperm(IO_PORT, 1, 1);
  108     if (ret < 0) {
  109         pr_err("%s: cannot access port 0x%x, errno = %d (%s)\n",
  110             args->name, IO_PORT, errno, strerror(errno));
  111         return EXIT_FAILURE;
  112     }
  113 
  114     v = inb(IO_PORT);
  115     do {
  116         if (flag & IOPORT_OPT_IN) {
  117             (void)inb(IO_PORT);
  118             (void)inb(IO_PORT);
  119             (void)inb(IO_PORT);
  120             (void)inb(IO_PORT);
  121             (void)inb(IO_PORT);
  122             (void)inb(IO_PORT);
  123             (void)inb(IO_PORT);
  124             (void)inb(IO_PORT);
  125             (void)inb(IO_PORT);
  126             (void)inb(IO_PORT);
  127             (void)inb(IO_PORT);
  128             (void)inb(IO_PORT);
  129             (void)inb(IO_PORT);
  130             (void)inb(IO_PORT);
  131             (void)inb(IO_PORT);
  132             (void)inb(IO_PORT);
  133         }
  134         if (flag & IOPORT_OPT_OUT) {
  135             outb(v, IO_PORT);
  136             outb(v, IO_PORT);
  137             outb(v, IO_PORT);
  138             outb(v, IO_PORT);
  139             outb(v, IO_PORT);
  140             outb(v, IO_PORT);
  141             outb(v, IO_PORT);
  142             outb(v, IO_PORT);
  143             outb(v, IO_PORT);
  144             outb(v, IO_PORT);
  145             outb(v, IO_PORT);
  146             outb(v, IO_PORT);
  147             outb(v, IO_PORT);
  148             outb(v, IO_PORT);
  149             outb(v, IO_PORT);
  150             outb(v, IO_PORT);
  151         }
  152         inc_counter(args);
  153     } while (keep_stressing());
  154 
  155     (void)ioperm(IO_PORT, 1, 0);
  156 
  157     return EXIT_SUCCESS;
  158 }
  159 
  160 stressor_info_t stress_ioport_info = {
  161     .stressor = stress_ioport,
  162     .supported = stress_ioport_supported,
  163     .class = CLASS_CPU
  164 };
  165 #else
  166 stressor_info_t stress_ioport_info = {
  167     .stressor = stress_not_implemented,
  168     .class = CLASS_CPU
  169 };
  170 #endif