/* SPDX-License-Identifier: GPL-2.0-only */
/*
 * Storage Key migration tests
 *
 * There are two variants of this test:
 * - sequential: set storage keys on some pages, migrates the VM and then
 *   verifies that the storage keys are still as we expect.
 * - parallel: start migration and set and check storage keys on some
 *   pages while migration is in process.
 *
 * Copyright IBM Corp. 2022
 *
 * Authors:
 *  Nico Boehr <nrb@linux.ibm.com>
 */

#include <libcflat.h>
#include <migrate.h>
#include <asm/facility.h>
#include <asm/page.h>
#include <asm/mem.h>
#include <asm/barrier.h>
#include <hardware.h>
#include <smp.h>

struct verify_result {
	bool verify_failed;
	union skey expected_key;
	union skey actual_key;
	unsigned long page_mismatch_idx;
	unsigned long page_mismatch_addr;
};

#define NUM_PAGES 128
static uint8_t pagebuf[NUM_PAGES * PAGE_SIZE] __attribute__((aligned(PAGE_SIZE)));

static struct verify_result result;

static unsigned int thread_iters;
static bool thread_should_exit;
static bool thread_exited;

static enum {
	TEST_INVALID,
	TEST_SEQUENTIAL,
	TEST_PARALLEL
} arg_test_to_run;

/*
 * Set storage key test pattern on pagebuf with a seed for the storage keys.
 *
 * Each page's storage key is generated by taking the page's index in pagebuf,
 * XOR-ing that with the given seed and then multipling the result with two.
 *
 * Only the lower seven bits of the seed are considered.
 */
static void set_test_pattern(unsigned char seed)
{
	unsigned char key_to_set;
	unsigned long i;

	for (i = 0; i < NUM_PAGES; i++) {
		/*
		 * Storage keys are 7 bit, lowest bit is always returned as zero
		 * by iske.
		 * This loop will set all 7 bits which means we set fetch
		 * protection as well as reference and change indication for
		 * some keys.
		 */
		key_to_set = (i ^ seed) * 2;
		set_storage_key(pagebuf + i * PAGE_SIZE, key_to_set, 1);
	}
}

/*
 * Verify storage keys on pagebuf.
 * Storage keys must have been set by set_test_pattern on pagebuf before.
 * set_test_pattern must have been called with the same seed value.
 *
 * If storage keys match the expected result, will return a verify_result
 * with verify_failed false. All other fields are then invalid.
 * If there is a mismatch, returned struct will have verify_failed true and will
 * be filled with the details on the first mismatch encountered.
 */
static struct verify_result verify_test_pattern(unsigned char seed)
{
	union skey expected_key, actual_key;
	struct verify_result result = {
		.verify_failed = true
	};
	uint8_t *cur_page;
	unsigned long i;

	for (i = 0; i < NUM_PAGES; i++) {
		cur_page = pagebuf + i * PAGE_SIZE;
		actual_key.val = get_storage_key(cur_page);
		expected_key.val = (i ^ seed) * 2;

		/*
		 * The PoP neither gives a guarantee that the reference bit is
		 * accurate nor that it won't be cleared by hardware. Hence we
		 * don't rely on it and just clear the bits to avoid compare
		 * errors.
		 */
		actual_key.str.rf = 0;
		expected_key.str.rf = 0;

		if (actual_key.val != expected_key.val) {
			result.expected_key.val = expected_key.val;
			result.actual_key.val = actual_key.val;
			result.page_mismatch_idx = i;
			result.page_mismatch_addr = (unsigned long)cur_page;
			return result;
		}
	}

	result.verify_failed = false;
	return result;
}

static void report_verify_result(const struct verify_result * result)
{
	if (result->verify_failed)
		report_fail("page skey mismatch: first page idx = %lu, addr = 0x%lx, "
			    "expected_key = 0x%02x, actual_key = 0x%02x",
			    result->page_mismatch_idx, result->page_mismatch_addr,
			    result->expected_key.val, result->actual_key.val);
	else
		report_pass("skeys match");
}

static void test_skey_migration_sequential(void)
{
	report_prefix_push("sequential");

	set_test_pattern(0);

	migrate_once();

	result = verify_test_pattern(0);
	report_verify_result(&result);

	report_prefix_pop();
}

static void set_skeys_thread(void)
{
	while (!READ_ONCE(thread_should_exit)) {
		set_test_pattern(thread_iters);

		result = verify_test_pattern(thread_iters);

		/*
		 * Always increment even if the verify fails. This ensures primary CPU knows where
		 * we left off and can do an additional verify round after migration finished.
		 */
		thread_iters++;

		if (result.verify_failed)
			break;
	}

	WRITE_ONCE(thread_exited, 1);
}

static void test_skey_migration_parallel(void)
{
	report_prefix_push("parallel");

	if (smp_query_num_cpus() == 1) {
		report_skip("need at least 2 cpus for this test");
		migrate_skip();
		goto error;
	}

	smp_cpu_setup(1, PSW_WITH_CUR_MASK(set_skeys_thread));

	migrate_once();

	WRITE_ONCE(thread_should_exit, 1);

	while (!READ_ONCE(thread_exited))
		;

	/* Ensure we read result and thread_iters below from memory after thread exited */
	mb();
	report_info("thread completed %u iterations", thread_iters);

	report_prefix_push("during migration");
	report_verify_result(&result);
	report_prefix_pop();

	/*
	 * Verification of skeys occurs on the thread. We don't know if we
	 * were still migrating during the verification.
	 * To be sure, make another verification round after the migration
	 * finished to catch skeys which might not have been migrated
	 * correctly.
	 */
	report_prefix_push("after migration");
	assert(thread_iters > 0);
	result = verify_test_pattern(thread_iters - 1);
	report_verify_result(&result);
	report_prefix_pop();

error:
	report_prefix_pop();
}

static void print_usage(void)
{
	report_info("Usage: migration-skey [--parallel|--sequential]");
}

static void parse_args(int argc, char **argv)
{
	if (argc < 2) {
		/* default to sequential since it only needs one cpu */
		arg_test_to_run = TEST_SEQUENTIAL;
		return;
	}

	if (!strcmp("--parallel", argv[1]))
		arg_test_to_run = TEST_PARALLEL;
	else if (!strcmp("--sequential", argv[1]))
		arg_test_to_run = TEST_SEQUENTIAL;
	else
		arg_test_to_run = TEST_INVALID;
}

int main(int argc, char **argv)
{
	report_prefix_push("migration-skey");

	if (test_facility(169)) {
		report_skip("storage key removal facility is active");
		migrate_skip();
		goto error;
	}

	parse_args(argc, argv);

	switch (arg_test_to_run) {
	case TEST_SEQUENTIAL:
		test_skey_migration_sequential();
		break;
	case TEST_PARALLEL:
		test_skey_migration_parallel();
		break;
	default:
		print_usage();
		migrate_skip();
		break;
	}

error:
	report_prefix_pop();
	return report_summary();
}
