1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
|
// Copyright (c) 2024, Qualcomm Innovation Center, Inc. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
#ifndef VERIFY_PRINT_ERROR
#define VERIFY_PRINT_ERROR
#endif // VERIFY_PRINT_ERROR
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include "AEEStdErr.h"
#include "AEEstd.h"
#include "HAP_farf.h"
#include "apps_std.h"
#include "fastrpc_common.h"
#include "fastrpc_config.h"
#include "apps_std_internal.h"
#include "verify.h"
#define CONFIG_PDDUMP "pddump"
#define CONFIG_RPCTIMEOUT "rpctimeout"
#define CONFIG_PERF_KERNEL "perfkernel"
#define CONFIG_PERF_DSP "perfdsp"
#define CONFIG_COLLECT_RUNTIME_FARF "collectRuntimeFARF"
#define CONFIG_COLLECT_RUNTIME_FARF_USERSPACE "collectUserspaceRuntimeFARF"
#define CONFIG_LOG_IREGION "logiregion"
#define CONFIG_QTF_TRACING "qtftracing"
#define CONFIG_CALLER_LEVEL "callerlevel"
#define CONFIG_ENABLE_UAF "uafenabled"
#define CONFIG_DEBUG_LOGGING "debuglogging"
#define CONFIG_DEBUG_SYSMON_LOGGING "sysmonreservedbit"
#define CONFIG_ERR_CODES "errcodes"
#define MIN_DSP_ERR_RNG 0x80000401
#define MAX_DSP_ERR_RNG 0x80000601
#define CONFIG_LOGPACKET "logPackets"
#define CONFIG_LEAK_DETECT "leak_detect"
#define CONFIG_CALL_STACK_NUM "num_call_stack"
#define CONFIG_SETDMABUFNAME "setdmabufname"
struct fastrpc_config_param {
bool pddump;
int rpc_timeout;
bool perfkernel;
bool perfdsp;
_cstring1_t *paths;
char *farf_log_filename;
char *farf_log_filename_userspace;
bool log_iregion;
bool qtf_tracing;
int caller_level;
bool uaf_enabled;
bool debug_logging;
struct err_codes err_codes_to_crash;
bool sysmonreservedbit;
bool logPackets;
int leak_detect;
int num_call_stack;
bool setdmabufname;
};
static struct fastrpc_config_param frpc_config;
// Function to read and parse config file
int fastrpc_read_config_file_from_path(const char *base, const char *file) {
int nErr = 0;
apps_std_FILE fp = -1;
uint64_t len;
unsigned char *buf = NULL;
int eof;
char *path = NULL, *param = NULL, *saveptr = NULL;
bool fileExists = false;
char *delim = "=", *delim2 = ",";
uint64_t logFileNameLen = 0;
frpc_config.err_codes_to_crash.num_err_codes = 0;
len = snprintf(0, 0, "%s/%s", base, file) + 1;
VERIFYC(NULL != (path = calloc(1, sizeof(char) * len)), AEE_ENOMEMORY);
snprintf(path, (int)len, "%s/%s", base, file);
VERIFY(AEE_SUCCESS == (nErr = apps_std_fileExists(path, &fileExists)));
if (fileExists == false) {
nErr = AEE_ENOSUCHFILE;
goto bail;
}
VERIFY(AEE_SUCCESS == (nErr = apps_std_fopen(path, "r", &fp)));
VERIFY(AEE_SUCCESS == (nErr = apps_std_flen(fp, &len)));
// Allocate buffer for reading each line
VERIFYC(NULL != (buf = calloc(1, sizeof(unsigned char) * (len + 1))),
AEE_ENOMEMORY); // extra 1 unsigned char for null character
do {
// Read each line at a time
VERIFY(AEE_SUCCESS == (nErr = apps_std_fgets(fp, buf, len, &eof)));
if (eof) {
break;
}
param = strtok_r((char *)buf, delim, &saveptr);
if (param == NULL) {
continue;
}
if (strncmp(param, CONFIG_ERR_CODES, strlen(CONFIG_ERR_CODES)) ==
0) {
int ii = 0, num_err_codes = 0;
unsigned int err_code = 0;
FARF(ALWAYS, "%s: panic error codes applicable only to cdsp domain\n",
__func__);
do {
param = strtok_r(NULL, delim2, &saveptr);
if (param != NULL) {
err_code = strtol(param, NULL, 16);
if (err_code >= MIN_DSP_ERR_RNG && err_code <= MAX_DSP_ERR_RNG) {
frpc_config.err_codes_to_crash.err_code[ii] = err_code;
FARF(ALWAYS, "%s : panic error codes : 0x%x\n", __func__,
frpc_config.err_codes_to_crash.err_code[ii]);
ii++;
if (ii >= MAX_PANIC_ERR_CODES) {
FARF(ERROR, "%s : Max panic error codes limit reached\n",
__func__, MAX_PANIC_ERR_CODES);
break;
}
} else {
FARF(ALWAYS,
"%s : panic error code read from debugcnfig : 0x%x is not in "
"dsp error codes range\n",
__func__, err_code);
}
}
} while (param != NULL);
num_err_codes = ii;
frpc_config.err_codes_to_crash.num_err_codes = num_err_codes;
} else if (strncmp(param, CONFIG_PDDUMP, strlen(CONFIG_PDDUMP)) ==
0) {
param = strtok_r(NULL, delim, &saveptr);
if (param != NULL && atoi(param)) {
frpc_config.pddump = true;
FARF(ALWAYS, "fastrpc config enabling PD dump\n");
}
} else if (strncmp(param, CONFIG_RPCTIMEOUT,
strlen(CONFIG_RPCTIMEOUT)) == 0) {
param = strtok_r(NULL, delim, &saveptr);
if (param != NULL) {
frpc_config.rpc_timeout = atoi(param);
FARF(ALWAYS, "fastrpc config set rpc timeout with %d\n",
frpc_config.rpc_timeout);
}
} else if (strncmp(param, CONFIG_PERF_KERNEL,
strlen(CONFIG_PERF_KERNEL)) == 0) {
param = strtok_r(NULL, delim, &saveptr);
if (param != NULL && atoi(param)) {
frpc_config.perfkernel = true;
FARF(ALWAYS, "fastrpc config enabling profiling on kernel\n");
}
} else if (strncmp(param, CONFIG_PERF_DSP,
strlen(CONFIG_PERF_DSP)) == 0) {
param = strtok_r(NULL, delim, &saveptr);
if (param != NULL && atoi(param)) {
frpc_config.perfdsp = true;
FARF(ALWAYS, "fastrpc config enabling profiling on dsp\n");
}
} else if (strncmp(param, CONFIG_COLLECT_RUNTIME_FARF,
strlen(CONFIG_COLLECT_RUNTIME_FARF)) == 0) {
param = strtok_r(NULL, delim, &saveptr);
if (param != NULL) {
logFileNameLen = strlen(param) + 1;
VERIFYC(NULL != (frpc_config.farf_log_filename =
(char *)malloc(sizeof(char) * logFileNameLen)),
AEE_ENOMEMORY);
strlcpy(frpc_config.farf_log_filename, param, logFileNameLen);
FARF(ALWAYS,
"fastrpc config enabling farf logs collection into file %s",
frpc_config.farf_log_filename);
}
} else if (strncmp(param, CONFIG_COLLECT_RUNTIME_FARF_USERSPACE,
strlen(CONFIG_COLLECT_RUNTIME_FARF_USERSPACE)) ==
0) {
param = strtok_r(NULL, delim, &saveptr);
if (param != NULL) {
logFileNameLen = strlen(param) + 1;
VERIFYC(NULL != (frpc_config.farf_log_filename_userspace =
(char *)malloc(sizeof(char) * logFileNameLen)),
AEE_ENOMEMORY);
strlcpy(frpc_config.farf_log_filename_userspace, param,
logFileNameLen);
FARF(ALWAYS,
"fastrpc config enabling userspace farf logs collection into file "
"%s",
frpc_config.farf_log_filename_userspace);
}
} else if (strncmp(param, CONFIG_LOG_IREGION,
strlen(CONFIG_LOG_IREGION)) == 0) {
param = strtok_r(NULL, delim, &saveptr);
if (param != NULL) {
frpc_config.log_iregion = true;
FARF(ALWAYS, "fastrpc config enabling iregion logging\n");
}
} else if (strncmp(param, CONFIG_QTF_TRACING,
strlen(CONFIG_QTF_TRACING)) == 0) {
param = strtok_r(NULL, delim, &saveptr);
if (param != NULL && atoi(param)) {
frpc_config.qtf_tracing = true;
FARF(ALWAYS, "fastrpc config enabling QTF tracing\n");
}
} else if (strncmp(param, CONFIG_CALLER_LEVEL,
strlen(CONFIG_CALLER_LEVEL)) == 0) {
param = strtok_r(NULL, delim, &saveptr);
if (param != NULL && atoi(param)) {
frpc_config.caller_level = atoi(param);
FARF(ALWAYS, "fastrpc config setting heap caller level with %d\n",
frpc_config.caller_level);
}
} else if (strncmp(param, CONFIG_ENABLE_UAF,
strlen(CONFIG_ENABLE_UAF)) == 0) {
param = strtok_r(NULL, delim, &saveptr);
if (param != NULL && atoi(param)) {
frpc_config.uaf_enabled = true;
FARF(ALWAYS, "fastrpc config enabling uaf on heap\n");
}
} else if (strncmp(param, CONFIG_DEBUG_LOGGING,
strlen(CONFIG_DEBUG_LOGGING)) == 0) {
param = strtok_r(NULL, delim, &saveptr);
if (param != NULL) {
frpc_config.debug_logging = true;
FARF(ALWAYS, "fastrpc config enabling debug logging\n");
}
} else if (strncmp(param, CONFIG_DEBUG_SYSMON_LOGGING,
strlen(CONFIG_DEBUG_SYSMON_LOGGING)) == 0) {
param = strtok_r(NULL, delim, &saveptr);
if (param != NULL) {
frpc_config.sysmonreservedbit = true;
FARF(ALWAYS, "fastrpc config enabling sysmon logging \n");
}
} else if (strncmp(param, CONFIG_LOGPACKET,
strlen(CONFIG_LOGPACKET)) == 0) {
param = strtok_r(NULL, delim, &saveptr);
if (param != NULL) {
frpc_config.logPackets = true;
FARF(ALWAYS, "fastrpc config enabling Log packets\n");
}
} else if (strncmp(param, CONFIG_LEAK_DETECT,
strlen(CONFIG_LEAK_DETECT)) == 0) {
param = strtok_r(NULL, delim, &saveptr);
if (param != NULL && atoi(param)) {
frpc_config.leak_detect = atoi(param);
FARF(ALWAYS, "fastrpc config enabling leak detect with %d\n",
frpc_config.leak_detect);
}
} else if (strncmp(param, CONFIG_CALL_STACK_NUM,
strlen(CONFIG_CALL_STACK_NUM)) == 0) {
param = strtok_r(NULL, delim, &saveptr);
if (param != NULL && atoi(param)) {
frpc_config.num_call_stack = atoi(param);
FARF(ALWAYS, "fastrpc config setting call stack num with %d\n",
frpc_config.num_call_stack);
}
} else if (strncmp(param, CONFIG_SETDMABUFNAME,
strlen(CONFIG_SETDMABUFNAME)) == 0) {
param = strtok_r (NULL, delim, &saveptr);
if (param != NULL && atoi(param))
frpc_config.setdmabufname = true;
}
param = NULL;
} while (!eof);
bail:
if (buf != NULL) {
free(buf);
buf = NULL;
}
if (fp != -1) {
apps_std_fclose(fp);
}
if (path != NULL) {
free(path);
path = NULL;
}
if (nErr != AEE_SUCCESS && nErr != AEE_ENOSUCHFILE) {
FARF(ALWAYS, "Error 0x%x: failed for %s/%s with errno(%s)\n", nErr, base,
file, strerror(errno));
}
return nErr;
}
// Function to get panic error codes.
struct err_codes *fastrpc_config_get_errcodes(void) {
if (frpc_config.err_codes_to_crash.num_err_codes != 0)
return (&(frpc_config.err_codes_to_crash));
return NULL;
}
// Function to get rpc timeout.
int fastrpc_config_get_rpctimeout(void) { return frpc_config.rpc_timeout; }
// Function to get if PD dump feature is enabled.
bool fastrpc_config_is_pddump_enabled(void) { return frpc_config.pddump; }
// Functions to get if profiling mode is enabled.
bool fastrpc_config_is_perfkernel_enabled(void) {
return frpc_config.perfkernel;
}
bool fastrpc_config_is_perfdsp_enabled(void) { return frpc_config.perfdsp; }
// Function to get the file name to collect runtime farf logs.
char *fastrpc_config_get_runtime_farf_file(void) {
return frpc_config.farf_log_filename;
}
// Function to get the file name to collect userspace runtime farf logs.
char *fastrpc_config_get_userspace_runtime_farf_file(void) {
return frpc_config.farf_log_filename_userspace;
}
// Function to get if iregion logging feature is enabled.
bool fastrpc_config_is_log_iregion_enabled(void) {
return frpc_config.log_iregion;
}
// Function to get if debug logging feature is enabled.
bool fastrpc_config_is_debug_logging_enabled(void) {
return frpc_config.debug_logging;
}
// Function to get if debug logging feature is enabled for sysmon reserved bit.
bool fastrpc_config_is_sysmon_reserved_bit_enabled(void) {
return frpc_config.sysmonreservedbit;
}
// Function to get if QTF tracing is enabled.
bool fastrpc_config_is_qtf_tracing_enabled(void) {
return frpc_config.qtf_tracing;
}
// Function to get heap caller level.
int fastrpc_config_get_caller_level(void) { return frpc_config.caller_level; }
// Function to get if uaf should be enabled in heap
bool fastrpc_config_is_uaf_enabled(void) { return frpc_config.uaf_enabled; }
// Function to get if Log packet is enabled.
bool fastrpc_config_is_logpacket_enabled(void) {
return frpc_config.logPackets;
}
// Function to get if leak detect is enabled
int fastrpc_config_get_leak_detect(void) { return frpc_config.leak_detect; }
// Function to return the call stack num
int fastrpc_config_get_caller_stack_num(void) {
return frpc_config.num_call_stack;
}
bool fastrpc_config_is_setdmabufname_enabled(void) {
return frpc_config.setdmabufname;
}
// Fastrpc config init function
int fastrpc_config_init() {
int nErr = AEE_SUCCESS, i = 0;
const char *file_extension = ".debugconfig";
char *name = NULL;
char *data_paths = NULL;
char *config_file = NULL;
_cstring1_t *paths = NULL;
uint32_t len = 0;
uint16_t maxPathLen = 0;
uint32_t numPaths = 0;
int file_found = 0;
VERIFYC(NULL != (name = std_basename(__progname)), AEE_EINVALIDPROCNAME);
len = strlen(name) + strlen(file_extension) + 1;
VERIFYC(NULL != (config_file = calloc(1, sizeof(char) * len)), AEE_ENOMEMORY);
// Prepare config filename
snprintf(config_file, len, "%s%s", name, file_extension);
FARF(RUNTIME_RPC_HIGH, "Reading configuration file: %s\n", config_file);
// Get the required size for PATH
apps_std_get_search_paths_with_env(ADSP_LIBRARY_PATH, ";", NULL, 0, &numPaths,
&maxPathLen);
maxPathLen += +1;
// Allocate memory for the PATH's
VERIFYC(NULL != (paths = calloc(1, sizeof(_cstring1_t) * numPaths)),
AEE_ENOMEMORY);
for (i = 0; i < (int)numPaths; ++i) {
VERIFYC(NULL != (paths[i].data = calloc(1, sizeof(char) * maxPathLen)),
AEE_ENOMEMORY);
paths[i].dataLen = maxPathLen;
}
// Allocate single buffer for all the PATHs
VERIFYC(NULL !=
(data_paths = calloc(1, sizeof(char) * maxPathLen * numPaths)),
AEE_ENOMEMORY);
// Get the paths
VERIFY(AEE_SUCCESS ==
(nErr = apps_std_get_search_paths_with_env(
ADSP_LIBRARY_PATH, ";", paths, numPaths, &len, &maxPathLen)));
maxPathLen += 1;
for (i = 0; i < (int)numPaths; ++i) {
strlcat(data_paths, paths[i].data,
sizeof(char) * maxPathLen * numPaths);
strlcat(data_paths, ", ", sizeof(char) * maxPathLen * numPaths);
if (0 == fastrpc_read_config_file_from_path(paths[i].data, config_file)) {
file_found = 1;
FARF(ALWAYS, "Read fastrpc config file %s found at %s\n", config_file,
paths[i].data);
break;
}
}
if (!file_found) {
FARF(RUNTIME_RPC_HIGH, "%s: Couldn't find file %s, errno (%s) at %s\n", __func__,
config_file, strerror(errno), data_paths);
}
bail:
if (nErr) {
FARF(ERROR, "%s: failed for process %s", __func__, name);
}
if (paths) {
for (i = 0; i < (int)numPaths; ++i) {
if (paths[i].data) {
free(paths[i].data);
paths[i].data = NULL;
}
}
free(paths);
paths = NULL;
}
if (config_file) {
free(config_file);
config_file = NULL;
}
if (data_paths) {
free(data_paths);
}
return nErr;
}
|