Home | History | Annotate | Line # | Download | only in ufetchstore
      1  1.1  christos /*	$NetBSD: ufetchstore_tester.c,v 1.1 2019/04/15 23:41:23 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2019 The NetBSD Foundation, Inc.
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  christos  * by Jason R. Thorpe.
      9  1.1  christos  *
     10  1.1  christos  * Redistribution and use in source and binary forms, with or without
     11  1.1  christos  * modification, are permitted provided that the following conditions
     12  1.1  christos  * are met:
     13  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  christos  *    documentation and/or other materials provided with the distribution.
     18  1.1  christos  *
     19  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  christos  */
     31  1.1  christos 
     32  1.1  christos #include <sys/types.h>
     33  1.1  christos #include <sys/module.h>
     34  1.1  christos #include <sys/sysctl.h>
     35  1.1  christos #include <sys/systm.h>
     36  1.1  christos 
     37  1.1  christos #include "common.h"
     38  1.1  christos 
     39  1.1  christos static struct tester_ctx {
     40  1.1  christos 	struct sysctllog *ctx_sysctllog;
     41  1.1  christos } tester_ctx;
     42  1.1  christos 
     43  1.1  christos static int
     44  1.1  christos test_ufetch(void * const uaddr, struct ufetchstore_test_args * const args)
     45  1.1  christos {
     46  1.1  christos 	int error = 0;
     47  1.1  christos 
     48  1.1  christos 	switch (args->size) {
     49  1.1  christos 	case 8:
     50  1.1  christos 		args->fetchstore_error = ufetch_8(uaddr, &args->val8);
     51  1.1  christos 		break;
     52  1.1  christos 	case 16:
     53  1.1  christos 		args->fetchstore_error = ufetch_16(uaddr, &args->val16);
     54  1.1  christos 		break;
     55  1.1  christos 	case 32:
     56  1.1  christos 		args->fetchstore_error = ufetch_32(uaddr, &args->val32);
     57  1.1  christos 		break;
     58  1.1  christos #ifdef _LP64
     59  1.1  christos 	case 64:
     60  1.1  christos 		args->fetchstore_error = ufetch_64(uaddr, &args->val64);
     61  1.1  christos 		break;
     62  1.1  christos #endif /* _LP64 */
     63  1.1  christos 	default:
     64  1.1  christos 		error = EINVAL;
     65  1.1  christos 	}
     66  1.1  christos 
     67  1.1  christos 	return error;
     68  1.1  christos }
     69  1.1  christos 
     70  1.1  christos static int
     71  1.1  christos test_ustore(void * const uaddr, struct ufetchstore_test_args * const args)
     72  1.1  christos {
     73  1.1  christos 	int error = 0;
     74  1.1  christos 
     75  1.1  christos 	switch (args->size) {
     76  1.1  christos 	case 8:
     77  1.1  christos 		args->fetchstore_error = ustore_8(uaddr, args->val8);
     78  1.1  christos 		break;
     79  1.1  christos 	case 16:
     80  1.1  christos 		args->fetchstore_error = ustore_16(uaddr, args->val16);
     81  1.1  christos 		break;
     82  1.1  christos 	case 32:
     83  1.1  christos 		args->fetchstore_error = ustore_32(uaddr, args->val32);
     84  1.1  christos 		break;
     85  1.1  christos #ifdef _LP64
     86  1.1  christos 	case 64:
     87  1.1  christos 		args->fetchstore_error = ustore_64(uaddr, args->val64);
     88  1.1  christos 		break;
     89  1.1  christos #endif /* _LP64 */
     90  1.1  christos 	default:
     91  1.1  christos 		error = EINVAL;
     92  1.1  christos 	}
     93  1.1  christos 
     94  1.1  christos 	return error;
     95  1.1  christos }
     96  1.1  christos 
     97  1.1  christos static int
     98  1.1  christos test_ucas(void * const uaddr, struct ufetchstore_test_args * const args)
     99  1.1  christos {
    100  1.1  christos 	int error = 0;
    101  1.1  christos 
    102  1.1  christos 	switch (args->size) {
    103  1.1  christos 	case 32:
    104  1.1  christos 		args->fetchstore_error = ucas_32(uaddr,
    105  1.1  christos 		    args->ea_val32, args->val32, &args->ea_val32);
    106  1.1  christos 		break;
    107  1.1  christos #ifdef _LP64
    108  1.1  christos 	case 64:
    109  1.1  christos 		args->fetchstore_error = ucas_64(uaddr,
    110  1.1  christos 		    args->ea_val64, args->val64, &args->ea_val64);
    111  1.1  christos 		break;
    112  1.1  christos #endif /* _LP64 */
    113  1.1  christos 	default:
    114  1.1  christos 		error = EINVAL;
    115  1.1  christos 	}
    116  1.1  christos 
    117  1.1  christos 	return error;
    118  1.1  christos }
    119  1.1  christos 
    120  1.1  christos static int
    121  1.1  christos do_ufetchstore_test(SYSCTLFN_ARGS)
    122  1.1  christos {
    123  1.1  christos 	struct sysctlnode node;
    124  1.1  christos 	struct ufetchstore_test_args *uargs, args;
    125  1.1  christos 	uint64_t args64;
    126  1.1  christos 	int error;
    127  1.1  christos 
    128  1.1  christos 	node = *rnode;
    129  1.1  christos 
    130  1.1  christos 	uargs = NULL;
    131  1.1  christos 	node.sysctl_data = &args64;
    132  1.1  christos 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    133  1.1  christos 	if (error)
    134  1.1  christos 		return error;
    135  1.1  christos 	if (newp == NULL)
    136  1.1  christos 		return EINVAL;
    137  1.1  christos 
    138  1.1  christos 	uargs = (void *)(uintptr_t)args64;
    139  1.1  christos 
    140  1.1  christos 	error = copyin(uargs, &args, sizeof(args));
    141  1.1  christos 	if (error)
    142  1.1  christos 		return error;
    143  1.1  christos 
    144  1.1  christos 	args.fetchstore_error = EBADF;	/* poison */
    145  1.1  christos 
    146  1.1  christos 	void *uaddr = (void *)(uintptr_t)args.uaddr64;
    147  1.1  christos 
    148  1.1  christos 	switch (args.test_op) {
    149  1.1  christos 	case OP_LOAD:
    150  1.1  christos 		error = test_ufetch(uaddr, &args);
    151  1.1  christos 		break;
    152  1.1  christos 
    153  1.1  christos 	case OP_STORE:
    154  1.1  christos 		error = test_ustore(uaddr, &args);
    155  1.1  christos 		break;
    156  1.1  christos 
    157  1.1  christos 	case OP_CAS:
    158  1.1  christos 		error = test_ucas(uaddr, &args);
    159  1.1  christos 		break;
    160  1.1  christos 
    161  1.1  christos 	default:
    162  1.1  christos 		error = EINVAL;
    163  1.1  christos 	}
    164  1.1  christos 
    165  1.1  christos 	if (error == 0)
    166  1.1  christos 		error = copyout(&args, uargs, sizeof(args));
    167  1.1  christos 	return error;
    168  1.1  christos }
    169  1.1  christos 
    170  1.1  christos static int
    171  1.1  christos ufetchstore_tester_init(void)
    172  1.1  christos {
    173  1.1  christos 	struct sysctllog **log = &tester_ctx.ctx_sysctllog;
    174  1.1  christos 	const struct sysctlnode *rnode, *cnode;
    175  1.1  christos 	int error;
    176  1.1  christos 
    177  1.1  christos 	error = sysctl_createv(log, 0, NULL, &rnode, CTLFLAG_PERMANENT,
    178  1.1  christos 	    CTLTYPE_NODE, "ufetchstore_test",
    179  1.1  christos 	    SYSCTL_DESCR("ufetchstore testing interface"),
    180  1.1  christos 	    NULL, 0, NULL, 0, CTL_KERN, CTL_CREATE, CTL_EOL);
    181  1.1  christos 	if (error)
    182  1.1  christos 		goto return_error;
    183  1.1  christos 
    184  1.1  christos 	error = sysctl_createv(log, 0, &rnode, &cnode,
    185  1.1  christos 	    /*
    186  1.1  christos 	     * It's really a pointer to our argument structure, because
    187  1.1  christos 	     * we want to have precise control over when copyin / copyout
    188  1.1  christos 	     * happens.
    189  1.1  christos 	     */
    190  1.1  christos 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_QUAD, "test",
    191  1.1  christos 	    SYSCTL_DESCR("execute a ufetchstore test"),
    192  1.1  christos 	    do_ufetchstore_test, 0,
    193  1.1  christos 	    (void *)&tester_ctx, 0, CTL_CREATE, CTL_EOL);
    194  1.1  christos 
    195  1.1  christos  return_error:
    196  1.1  christos  	if (error)
    197  1.1  christos 		sysctl_teardown(log);
    198  1.1  christos 	return error;
    199  1.1  christos }
    200  1.1  christos 
    201  1.1  christos static int
    202  1.1  christos ufetchstore_tester_fini(void)
    203  1.1  christos {
    204  1.1  christos 	sysctl_teardown(&tester_ctx.ctx_sysctllog);
    205  1.1  christos 	return 0;
    206  1.1  christos }
    207  1.1  christos 
    208  1.1  christos static int
    209  1.1  christos ufetchstore_tester_modcmd(modcmd_t cmd, void *arg __unused)
    210  1.1  christos {
    211  1.1  christos 	int error;
    212  1.1  christos 
    213  1.1  christos 	switch (cmd) {
    214  1.1  christos 	case MODULE_CMD_INIT:
    215  1.1  christos 		error = ufetchstore_tester_init();
    216  1.1  christos 		break;
    217  1.1  christos 
    218  1.1  christos 	case MODULE_CMD_FINI:
    219  1.1  christos 		error = ufetchstore_tester_fini();
    220  1.1  christos 		break;
    221  1.1  christos 
    222  1.1  christos 	case MODULE_CMD_STAT:
    223  1.1  christos 	default:
    224  1.1  christos 		error = ENOTTY;
    225  1.1  christos 	}
    226  1.1  christos 
    227  1.1  christos 	return error;
    228  1.1  christos }
    229  1.1  christos 
    230  1.1  christos MODULE(MODULE_CLASS_MISC, ufetchstore_tester, NULL);
    231