Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, Linaro Limited |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | * this list of conditions and the following disclaimer in the documentation |
| 13 | * and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 25 | * POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
Igor Opaniuk | 55fcc4a | 2018-02-06 19:20:22 +0200 | [diff] [blame] | 27 | #include <fcntl.h> |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 28 | #include <libgen.h> |
| 29 | #include <linux/limits.h> |
| 30 | #include <math.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <stdio.h> |
| 33 | #include <string.h> |
Igor Opaniuk | 55fcc4a | 2018-02-06 19:20:22 +0200 | [diff] [blame] | 34 | #include <sys/mman.h> |
| 35 | #include <sys/stat.h> |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 36 | #include <unistd.h> |
| 37 | |
| 38 | #include "benchmark_aux.h" |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 39 | #include "common.h" |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 40 | |
| 41 | /* Misc auxilary functions */ |
| 42 | void tee_errx(const char *msg, TEEC_Result res) |
| 43 | { |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 44 | ERROR_EXIT("%s: 0x%08x\n", msg, res); |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void tee_check_res(TEEC_Result res, const char *errmsg) |
| 48 | { |
| 49 | if (res != TEEC_SUCCESS) |
| 50 | tee_errx(errmsg, res); |
| 51 | } |
| 52 | |
| 53 | const char *bench_str_src(uint64_t source) |
| 54 | { |
| 55 | switch (source) { |
| 56 | case TEE_BENCH_CORE: |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 57 | return "core"; |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 58 | case TEE_BENCH_KMOD: |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 59 | return "kmodule"; |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 60 | case TEE_BENCH_CLIENT: |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 61 | return "libteec"; |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 62 | case TEE_BENCH_UTEE: |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 63 | return "libutee"; |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 64 | default: |
Igor Opaniuk | f1f3fd0 | 2017-09-14 15:34:56 +0300 | [diff] [blame] | 65 | return "-"; |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | void print_line(void) |
| 70 | { |
| 71 | int n = 115; |
| 72 | |
| 73 | while (n-- > 0) |
| 74 | printf("="); |
| 75 | printf("\n"); |
| 76 | } |
| 77 | |
| 78 | void alloc_argv(int argc, char *argv[], char **new_argv[]) |
| 79 | { |
| 80 | char *res, *base; |
| 81 | char path[PATH_MAX]; |
| 82 | char **testapp_argv; |
l00176142 | d1cfd65 | 2017-11-23 00:52:28 +0800 | [diff] [blame] | 83 | int i; |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 84 | |
| 85 | res = realpath(argv[1], path); |
| 86 | if (!res) |
| 87 | _exit(EXIT_FAILURE); |
| 88 | |
| 89 | *new_argv = malloc(argc * sizeof(void *)); |
| 90 | if (!(*new_argv)) |
| 91 | _exit(EXIT_FAILURE); |
| 92 | |
| 93 | testapp_argv = *new_argv; |
| 94 | testapp_argv[argc-1] = NULL; |
| 95 | base = basename(path); |
| 96 | |
| 97 | testapp_argv[0] = malloc(strlen(base) + 1); |
| 98 | if (!testapp_argv[0]) |
| 99 | _exit(EXIT_FAILURE); |
| 100 | |
| 101 | memcpy(testapp_argv[0], base, strlen(base) + 1); |
| 102 | |
l00176142 | d1cfd65 | 2017-11-23 00:52:28 +0800 | [diff] [blame] | 103 | for (i = 2; i < argc; i++) { |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 104 | size_t length = strlen(argv[i]) + 1; |
| 105 | |
| 106 | testapp_argv[i - 1] = malloc(length); |
| 107 | if (!testapp_argv[i - 1]) |
| 108 | _exit(EXIT_FAILURE); |
| 109 | |
| 110 | memcpy(testapp_argv[i-1], argv[i], length); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void dealloc_argv(int new_argc, char **new_argv) |
| 115 | { |
l00176142 | d1cfd65 | 2017-11-23 00:52:28 +0800 | [diff] [blame] | 116 | int i; |
| 117 | for (i = 0; i < new_argc; ++i) |
Igor Opaniuk | ab88c95 | 2017-02-14 13:22:54 +0200 | [diff] [blame] | 118 | free(new_argv[i]); |
| 119 | |
| 120 | free(new_argv); |
| 121 | } |
| 122 | |
| 123 | uint32_t get_cores(void) |
| 124 | { |
| 125 | return sysconf(_SC_NPROCESSORS_ONLN); |
| 126 | } |
Igor Opaniuk | 55fcc4a | 2018-02-06 19:20:22 +0200 | [diff] [blame] | 127 | |
| 128 | void *mmap_paddr(intptr_t paddr, uint64_t size) |
| 129 | { |
| 130 | int devmem; |
| 131 | off_t offset = 0; |
| 132 | off_t page_addr; |
| 133 | intptr_t *hw_addr = (intptr_t *)paddr; |
| 134 | |
| 135 | devmem = open("/dev/mem", O_RDWR); |
| 136 | if (!devmem) |
| 137 | return NULL; |
| 138 | |
Igor Opaniuk | 845a783 | 2018-06-06 14:06:10 +0300 | [diff] [blame] | 139 | offset = (off_t)(uintptr_t)hw_addr % getpagesize(); |
| 140 | page_addr = (off_t)(uintptr_t)(hw_addr - offset); |
Igor Opaniuk | 55fcc4a | 2018-02-06 19:20:22 +0200 | [diff] [blame] | 141 | |
| 142 | hw_addr = (intptr_t *)mmap(0, size, PROT_READ|PROT_WRITE, |
| 143 | MAP_SHARED, devmem, page_addr); |
| 144 | if (hw_addr == MAP_FAILED) { |
| 145 | close(devmem); |
| 146 | return NULL; |
| 147 | } |
| 148 | |
| 149 | close(devmem); |
| 150 | return (hw_addr + offset); |
| 151 | } |
Igor Opaniuk | 3d566ae | 2018-01-29 02:41:33 +0200 | [diff] [blame] | 152 | |
| 153 | size_t get_library_load_offset(pid_t pid, const char *libname) |
| 154 | { |
| 155 | char path[256]; |
| 156 | char buf[256]; |
| 157 | FILE *file; |
| 158 | size_t addr = 0; |
| 159 | size_t start, end, offset; |
| 160 | char flags[4]; |
| 161 | int len; |
| 162 | int len_libname = strlen(libname); |
| 163 | |
| 164 | snprintf(path, sizeof(path), "/proc/%d/smaps", pid); |
| 165 | |
| 166 | file = fopen(path, "rt"); |
| 167 | if (file == NULL) |
| 168 | return 0; |
| 169 | |
| 170 | while (fgets(buf, sizeof(buf), file) != NULL) { |
| 171 | len = strlen(buf); |
| 172 | if (len > 0 && buf[len-1] == '\n') |
| 173 | buf[--len] = '\0'; |
| 174 | |
| 175 | if (len <= len_libname || !strstr(buf, libname)) |
| 176 | continue; |
| 177 | |
Igor Opaniuk | 3d566ae | 2018-01-29 02:41:33 +0200 | [diff] [blame] | 178 | if (sscanf(buf, "%zx-%zx %c%c%c%c %zx", &start, &end, |
| 179 | &flags[0], &flags[1], |
| 180 | &flags[2], &flags[3], &offset) != 7) |
| 181 | continue; |
| 182 | |
| 183 | if (flags[0] != 'r' || flags[2] != 'x') |
| 184 | continue; |
| 185 | |
| 186 | addr = start - offset; |
| 187 | break; |
| 188 | } |
| 189 | |
| 190 | fclose(file); |
| 191 | |
| 192 | return addr; |
| 193 | } |
| 194 | |