cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

dscr_sysfs_test.c (1979B)


      1// SPDX-License-Identifier: GPL-2.0-only
      2/*
      3 * POWER Data Stream Control Register (DSCR) sysfs interface test
      4 *
      5 * This test updates to system wide DSCR default through the sysfs interface
      6 * and then verifies that all the CPU specific DSCR defaults are updated as
      7 * well verified from their sysfs interfaces.
      8 *
      9 * Copyright 2015, Anshuman Khandual, IBM Corporation.
     10 */
     11#include "dscr.h"
     12
     13static int check_cpu_dscr_default(char *file, unsigned long val)
     14{
     15	char buf[10];
     16	int fd, rc;
     17
     18	fd = open(file, O_RDWR);
     19	if (fd == -1) {
     20		perror("open() failed");
     21		return 1;
     22	}
     23
     24	rc = read(fd, buf, sizeof(buf));
     25	if (rc == -1) {
     26		perror("read() failed");
     27		return 1;
     28	}
     29	close(fd);
     30
     31	buf[rc] = '\0';
     32	if (strtol(buf, NULL, 16) != val) {
     33		printf("DSCR match failed: %ld (system) %ld (cpu)\n",
     34					val, strtol(buf, NULL, 16));
     35		return 1;
     36	}
     37	return 0;
     38}
     39
     40static int check_all_cpu_dscr_defaults(unsigned long val)
     41{
     42	DIR *sysfs;
     43	struct dirent *dp;
     44	char file[LEN_MAX];
     45
     46	sysfs = opendir(CPU_PATH);
     47	if (!sysfs) {
     48		perror("opendir() failed");
     49		return 1;
     50	}
     51
     52	while ((dp = readdir(sysfs))) {
     53		int len;
     54
     55		if (!(dp->d_type & DT_DIR))
     56			continue;
     57		if (!strcmp(dp->d_name, "cpuidle"))
     58			continue;
     59		if (!strstr(dp->d_name, "cpu"))
     60			continue;
     61
     62		len = snprintf(file, LEN_MAX, "%s%s/dscr", CPU_PATH, dp->d_name);
     63		if (len >= LEN_MAX)
     64			continue;
     65		if (access(file, F_OK))
     66			continue;
     67
     68		if (check_cpu_dscr_default(file, val))
     69			return 1;
     70	}
     71	closedir(sysfs);
     72	return 0;
     73}
     74
     75int dscr_sysfs(void)
     76{
     77	unsigned long orig_dscr_default;
     78	int i, j;
     79
     80	SKIP_IF(!have_hwcap2(PPC_FEATURE2_DSCR));
     81
     82	orig_dscr_default = get_default_dscr();
     83	for (i = 0; i < COUNT; i++) {
     84		for (j = 0; j < DSCR_MAX; j++) {
     85			set_default_dscr(j);
     86			if (check_all_cpu_dscr_defaults(j))
     87				goto fail;
     88		}
     89	}
     90	set_default_dscr(orig_dscr_default);
     91	return 0;
     92fail:
     93	set_default_dscr(orig_dscr_default);
     94	return 1;
     95}
     96
     97int main(int argc, char *argv[])
     98{
     99	return test_harness(dscr_sysfs, "dscr_sysfs_test");
    100}