2 * This file is part of the SPL: Solaris Porting Layer.
4 * Copyright (c) 2008 Lawrence Livermore National Security, LLC.
5 * Produced at Lawrence Livermore National Laboratory
7 * Brian Behlendorf <behlendorf1@llnl.gov>,
8 * Herb Wartens <wartens2@llnl.gov>,
9 * Jim Garlick <garlick@llnl.gov>
12 * This is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 * My intent is to create a loadable 'splat' (Solaris Porting LAyer
29 * Tests) module which can be used as an access point to run
30 * in kernel Solaris ABI regression tests. This provides a
31 * nice mechanism to validate the shim primates are working properly.
33 * The basic design is the splat module is that it is constructed of
34 * various splat_* source files each of which contains regression tests.
35 * For example the splat_linux_kmem.c file contains tests for validating
36 * kmem correctness. When the splat module is loaded splat_*_init()
37 * will be called for each subsystems tests, similarly splat_*_fini() is
38 * called when the splat module is removed. Each test can then be
39 * run by making an ioctl() call from a userspace control application
40 * to pick the subsystem and test which should be run.
43 #include "splat-internal.h"
45 static spl_class *splat_class;
46 static spl_device *splat_device;
47 static struct list_head splat_module_list;
48 static spinlock_t splat_module_lock;
51 splat_open(struct inode *inode, struct file *file)
53 unsigned int minor = iminor(inode);
56 if (minor >= SPLAT_MINORS)
59 info = (splat_info_t *)kmalloc(sizeof(*info), GFP_KERNEL);
63 spin_lock_init(&info->info_lock);
64 info->info_size = SPLAT_INFO_BUFFER_SIZE;
65 info->info_buffer = (char *)vmalloc(SPLAT_INFO_BUFFER_SIZE);
66 if (info->info_buffer == NULL) {
71 info->info_head = info->info_buffer;
72 file->private_data = (void *)info;
78 splat_release(struct inode *inode, struct file *file)
80 unsigned int minor = iminor(inode);
81 splat_info_t *info = (splat_info_t *)file->private_data;
83 if (minor >= SPLAT_MINORS)
87 ASSERT(info->info_buffer);
89 vfree(info->info_buffer);
96 splat_buffer_clear(struct file *file, splat_cfg_t *kcfg, unsigned long arg)
98 splat_info_t *info = (splat_info_t *)file->private_data;
101 ASSERT(info->info_buffer);
103 spin_lock(&info->info_lock);
104 memset(info->info_buffer, 0, info->info_size);
105 info->info_head = info->info_buffer;
106 spin_unlock(&info->info_lock);
112 splat_buffer_size(struct file *file, splat_cfg_t *kcfg, unsigned long arg)
114 splat_info_t *info = (splat_info_t *)file->private_data;
116 int min, size, rc = 0;
119 ASSERT(info->info_buffer);
121 spin_lock(&info->info_lock);
122 if (kcfg->cfg_arg1 > 0) {
124 size = kcfg->cfg_arg1;
125 buf = (char *)vmalloc(size);
131 /* Zero fill and truncate contents when coping buffer */
132 min = ((size < info->info_size) ? size : info->info_size);
133 memset(buf, 0, size);
134 memcpy(buf, info->info_buffer, min);
135 vfree(info->info_buffer);
136 info->info_size = size;
137 info->info_buffer = buf;
138 info->info_head = info->info_buffer;
141 kcfg->cfg_rc1 = info->info_size;
143 if (copy_to_user((struct splat_cfg_t __user *)arg, kcfg, sizeof(*kcfg)))
146 spin_unlock(&info->info_lock);
152 static splat_subsystem_t *
153 splat_subsystem_find(int id) {
154 splat_subsystem_t *sub;
156 spin_lock(&splat_module_lock);
157 list_for_each_entry(sub, &splat_module_list, subsystem_list) {
158 if (id == sub->desc.id) {
159 spin_unlock(&splat_module_lock);
163 spin_unlock(&splat_module_lock);
169 splat_subsystem_count(splat_cfg_t *kcfg, unsigned long arg)
171 splat_subsystem_t *sub;
174 spin_lock(&splat_module_lock);
175 list_for_each_entry(sub, &splat_module_list, subsystem_list)
178 spin_unlock(&splat_module_lock);
181 if (copy_to_user((struct splat_cfg_t __user *)arg, kcfg, sizeof(*kcfg)))
188 splat_subsystem_list(splat_cfg_t *kcfg, unsigned long arg)
190 splat_subsystem_t *sub;
194 /* Structure will be sized large enough for N subsystem entries
195 * which is passed in by the caller. On exit the number of
196 * entries filled in with valid subsystems will be stored in
197 * cfg_rc1. If the caller does not provide enough entries
198 * for all subsystems we will truncate the list to avoid overrun.
200 size = sizeof(*tmp) + kcfg->cfg_data.splat_subsystems.size *
201 sizeof(splat_user_t);
202 tmp = kmalloc(size, GFP_KERNEL);
206 /* Local 'tmp' is used as the structure copied back to user space */
207 memset(tmp, 0, size);
208 memcpy(tmp, kcfg, sizeof(*kcfg));
210 spin_lock(&splat_module_lock);
211 list_for_each_entry(sub, &splat_module_list, subsystem_list) {
212 strncpy(tmp->cfg_data.splat_subsystems.descs[i].name,
213 sub->desc.name, SPLAT_NAME_SIZE);
214 strncpy(tmp->cfg_data.splat_subsystems.descs[i].desc,
215 sub->desc.desc, SPLAT_DESC_SIZE);
216 tmp->cfg_data.splat_subsystems.descs[i].id = sub->desc.id;
218 /* Truncate list if we are about to overrun alloc'ed memory */
219 if ((i++) == kcfg->cfg_data.splat_subsystems.size)
222 spin_unlock(&splat_module_lock);
225 if (copy_to_user((struct splat_cfg_t __user *)arg, tmp, size)) {
235 splat_test_count(splat_cfg_t *kcfg, unsigned long arg)
237 splat_subsystem_t *sub;
241 /* Subsystem ID passed as arg1 */
242 sub = splat_subsystem_find(kcfg->cfg_arg1);
246 spin_lock(&(sub->test_lock));
247 list_for_each_entry(test, &(sub->test_list), test_list)
250 spin_unlock(&(sub->test_lock));
253 if (copy_to_user((struct splat_cfg_t __user *)arg, kcfg, sizeof(*kcfg)))
260 splat_test_list(splat_cfg_t *kcfg, unsigned long arg)
262 splat_subsystem_t *sub;
267 /* Subsystem ID passed as arg1 */
268 sub = splat_subsystem_find(kcfg->cfg_arg1);
272 /* Structure will be sized large enough for N test entries
273 * which is passed in by the caller. On exit the number of
274 * entries filled in with valid tests will be stored in
275 * cfg_rc1. If the caller does not provide enough entries
276 * for all tests we will truncate the list to avoid overrun.
278 size = sizeof(*tmp)+kcfg->cfg_data.splat_tests.size*sizeof(splat_user_t);
279 tmp = kmalloc(size, GFP_KERNEL);
283 /* Local 'tmp' is used as the structure copied back to user space */
284 memset(tmp, 0, size);
285 memcpy(tmp, kcfg, sizeof(*kcfg));
287 spin_lock(&(sub->test_lock));
288 list_for_each_entry(test, &(sub->test_list), test_list) {
289 strncpy(tmp->cfg_data.splat_tests.descs[i].name,
290 test->desc.name, SPLAT_NAME_SIZE);
291 strncpy(tmp->cfg_data.splat_tests.descs[i].desc,
292 test->desc.desc, SPLAT_DESC_SIZE);
293 tmp->cfg_data.splat_tests.descs[i].id = test->desc.id;
295 /* Truncate list if we are about to overrun alloc'ed memory */
296 if ((i++) == kcfg->cfg_data.splat_tests.size)
299 spin_unlock(&(sub->test_lock));
302 if (copy_to_user((struct splat_cfg_t __user *)arg, tmp, size)) {
312 splat_validate(struct file *file, splat_subsystem_t *sub, int cmd, void *arg)
316 spin_lock(&(sub->test_lock));
317 list_for_each_entry(test, &(sub->test_list), test_list) {
318 if (test->desc.id == cmd) {
319 spin_unlock(&(sub->test_lock));
320 return test->test(file, arg);
323 spin_unlock(&(sub->test_lock));
329 splat_ioctl_cfg(struct file *file, unsigned int cmd, unsigned long arg)
334 /* User and kernel space agree about arg size */
335 if (_IOC_SIZE(cmd) != sizeof(kcfg))
338 if (copy_from_user(&kcfg, (splat_cfg_t *)arg, sizeof(kcfg)))
341 if (kcfg.cfg_magic != SPLAT_CFG_MAGIC) {
342 splat_print(file, "Bad config magic 0x%x != 0x%x\n",
343 kcfg.cfg_magic, SPLAT_CFG_MAGIC);
347 switch (kcfg.cfg_cmd) {
348 case SPLAT_CFG_BUFFER_CLEAR:
352 rc = splat_buffer_clear(file, &kcfg, arg);
354 case SPLAT_CFG_BUFFER_SIZE:
355 /* cfg_arg1 - 0 - query size; >0 resize
356 * cfg_rc1 - Set to current buffer size
358 rc = splat_buffer_size(file, &kcfg, arg);
360 case SPLAT_CFG_SUBSYSTEM_COUNT:
362 * cfg_rc1 - Set to number of subsystems
364 rc = splat_subsystem_count(&kcfg, arg);
366 case SPLAT_CFG_SUBSYSTEM_LIST:
368 * cfg_rc1 - Set to number of subsystems
369 * cfg_data.splat_subsystems - Set with subsystems
371 rc = splat_subsystem_list(&kcfg, arg);
373 case SPLAT_CFG_TEST_COUNT:
374 /* cfg_arg1 - Set to a target subsystem
375 * cfg_rc1 - Set to number of tests
377 rc = splat_test_count(&kcfg, arg);
379 case SPLAT_CFG_TEST_LIST:
380 /* cfg_arg1 - Set to a target subsystem
381 * cfg_rc1 - Set to number of tests
382 * cfg_data.splat_subsystems - Populated with tests
384 rc = splat_test_list(&kcfg, arg);
387 splat_print(file, "Bad config command %d\n",
397 splat_ioctl_cmd(struct file *file, unsigned int cmd, unsigned long arg)
399 splat_subsystem_t *sub;
404 /* User and kernel space agree about arg size */
405 if (_IOC_SIZE(cmd) != sizeof(kcmd))
408 if (copy_from_user(&kcmd, (splat_cfg_t *)arg, sizeof(kcmd)))
411 if (kcmd.cmd_magic != SPLAT_CMD_MAGIC) {
412 splat_print(file, "Bad command magic 0x%x != 0x%x\n",
413 kcmd.cmd_magic, SPLAT_CFG_MAGIC);
417 /* Allocate memory for any opaque data the caller needed to pass on */
418 if (kcmd.cmd_data_size > 0) {
419 data = (void *)kmalloc(kcmd.cmd_data_size, GFP_KERNEL);
423 if (copy_from_user(data, (void *)(arg + offsetof(splat_cmd_t,
424 cmd_data_str)), kcmd.cmd_data_size)) {
430 sub = splat_subsystem_find(kcmd.cmd_subsystem);
432 rc = splat_validate(file, sub, kcmd.cmd_test, data);
443 splat_ioctl(struct inode *inode, struct file *file,
444 unsigned int cmd, unsigned long arg)
446 unsigned int minor = iminor(file->f_dentry->d_inode);
449 /* Ignore tty ioctls */
450 if ((cmd & 0xffffff00) == ((int)'T') << 8)
453 if (minor >= SPLAT_MINORS)
458 rc = splat_ioctl_cfg(file, cmd, arg);
461 rc = splat_ioctl_cmd(file, cmd, arg);
464 splat_print(file, "Bad ioctl command %d\n", cmd);
473 /* Compatibility handler for ioctls from 32-bit ELF binaries */
475 splat_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
477 return splat_ioctl(NULL, file, cmd, arg);
479 #endif /* CONFIG_COMPAT */
481 /* I'm not sure why you would want to write in to this buffer from
482 * user space since its principle use is to pass test status info
483 * back to the user space, but I don't see any reason to prevent it.
485 static ssize_t splat_write(struct file *file, const char __user *buf,
486 size_t count, loff_t *ppos)
488 unsigned int minor = iminor(file->f_dentry->d_inode);
489 splat_info_t *info = (splat_info_t *)file->private_data;
492 if (minor >= SPLAT_MINORS)
496 ASSERT(info->info_buffer);
498 spin_lock(&info->info_lock);
500 /* Write beyond EOF */
501 if (*ppos >= info->info_size) {
506 /* Resize count if beyond EOF */
507 if (*ppos + count > info->info_size)
508 count = info->info_size - *ppos;
510 if (copy_from_user(info->info_buffer, buf, count)) {
518 spin_unlock(&info->info_lock);
522 static ssize_t splat_read(struct file *file, char __user *buf,
523 size_t count, loff_t *ppos)
525 unsigned int minor = iminor(file->f_dentry->d_inode);
526 splat_info_t *info = (splat_info_t *)file->private_data;
529 if (minor >= SPLAT_MINORS)
533 ASSERT(info->info_buffer);
535 spin_lock(&info->info_lock);
537 /* Read beyond EOF */
538 if (*ppos >= info->info_size)
541 /* Resize count if beyond EOF */
542 if (*ppos + count > info->info_size)
543 count = info->info_size - *ppos;
545 if (copy_to_user(buf, info->info_buffer + *ppos, count)) {
553 spin_unlock(&info->info_lock);
557 static loff_t splat_seek(struct file *file, loff_t offset, int origin)
559 unsigned int minor = iminor(file->f_dentry->d_inode);
560 splat_info_t *info = (splat_info_t *)file->private_data;
563 if (minor >= SPLAT_MINORS)
567 ASSERT(info->info_buffer);
569 spin_lock(&info->info_lock);
572 case 0: /* SEEK_SET - No-op just do it */
574 case 1: /* SEEK_CUR - Seek from current */
575 offset = file->f_pos + offset;
577 case 2: /* SEEK_END - Seek from end */
578 offset = info->info_size + offset;
583 file->f_pos = offset;
588 spin_unlock(&info->info_lock);
593 static struct cdev splat_cdev;
594 static struct file_operations splat_fops = {
595 .owner = THIS_MODULE,
597 .release = splat_release,
598 .ioctl = splat_ioctl,
600 .compat_ioctl = splat_compat_ioctl,
603 .write = splat_write,
604 .llseek = splat_seek,
613 spin_lock_init(&splat_module_lock);
614 INIT_LIST_HEAD(&splat_module_list);
616 SPLAT_SUBSYSTEM_INIT(kmem);
617 SPLAT_SUBSYSTEM_INIT(taskq);
618 SPLAT_SUBSYSTEM_INIT(krng);
619 SPLAT_SUBSYSTEM_INIT(mutex);
620 SPLAT_SUBSYSTEM_INIT(condvar);
621 SPLAT_SUBSYSTEM_INIT(thread);
622 SPLAT_SUBSYSTEM_INIT(rwlock);
623 SPLAT_SUBSYSTEM_INIT(time);
624 SPLAT_SUBSYSTEM_INIT(vnode);
625 SPLAT_SUBSYSTEM_INIT(kobj);
626 SPLAT_SUBSYSTEM_INIT(atomic);
627 SPLAT_SUBSYSTEM_INIT(list);
628 SPLAT_SUBSYSTEM_INIT(generic);
629 SPLAT_SUBSYSTEM_INIT(cred);
631 dev = MKDEV(SPLAT_MAJOR, 0);
632 if ((rc = register_chrdev_region(dev, SPLAT_MINORS, SPLAT_NAME)))
635 /* Support for registering a character driver */
636 cdev_init(&splat_cdev, &splat_fops);
637 splat_cdev.owner = THIS_MODULE;
638 kobject_set_name(&splat_cdev.kobj, SPLAT_NAME);
639 if ((rc = cdev_add(&splat_cdev, dev, SPLAT_MINORS))) {
640 printk(KERN_ERR "SPLAT: Error adding cdev, %d\n", rc);
641 kobject_put(&splat_cdev.kobj);
642 unregister_chrdev_region(dev, SPLAT_MINORS);
646 /* Support for udev make driver info available in sysfs */
647 splat_class = spl_class_create(THIS_MODULE, "splat");
648 if (IS_ERR(splat_class)) {
649 rc = PTR_ERR(splat_class);
650 printk(KERN_ERR "SPLAT: Error creating splat class, %d\n", rc);
651 cdev_del(&splat_cdev);
652 unregister_chrdev_region(dev, SPLAT_MINORS);
656 splat_device = spl_device_create(splat_class, NULL,
657 MKDEV(SPLAT_MAJOR, 0),
660 printk(KERN_INFO "SPLAT: Loaded Solaris Porting LAyer "
661 "Tests v%s\n", SPL_META_VERSION);
664 printk(KERN_ERR "SPLAT: Error registering splat device, %d\n", rc);
671 dev_t dev = MKDEV(SPLAT_MAJOR, 0);
673 spl_device_destroy(splat_class, splat_device, dev);
674 spl_class_destroy(splat_class);
675 cdev_del(&splat_cdev);
676 unregister_chrdev_region(dev, SPLAT_MINORS);
678 SPLAT_SUBSYSTEM_FINI(cred);
679 SPLAT_SUBSYSTEM_FINI(generic);
680 SPLAT_SUBSYSTEM_FINI(list);
681 SPLAT_SUBSYSTEM_FINI(atomic);
682 SPLAT_SUBSYSTEM_FINI(kobj);
683 SPLAT_SUBSYSTEM_FINI(vnode);
684 SPLAT_SUBSYSTEM_FINI(time);
685 SPLAT_SUBSYSTEM_FINI(rwlock);
686 SPLAT_SUBSYSTEM_FINI(thread);
687 SPLAT_SUBSYSTEM_FINI(condvar);
688 SPLAT_SUBSYSTEM_FINI(mutex);
689 SPLAT_SUBSYSTEM_FINI(krng);
690 SPLAT_SUBSYSTEM_FINI(taskq);
691 SPLAT_SUBSYSTEM_FINI(kmem);
693 ASSERT(list_empty(&splat_module_list));
694 printk(KERN_INFO "SPLAT: Unloaded Solaris Porting LAyer "
695 "Tests v%s\n", SPL_META_VERSION);
700 spl_module_init(splat_init);
701 spl_module_exit(splat_fini);
703 MODULE_AUTHOR("Lawrence Livermore National Labs");
704 MODULE_DESCRIPTION("Solaris Porting LAyer Tests");
705 MODULE_LICENSE("GPL");