2019-05-19 20:08:20 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* arch/um/drivers/mmapper_kern.c
|
|
|
|
*
|
|
|
|
* BRIEF MODULE DESCRIPTION
|
|
|
|
*
|
|
|
|
* Copyright (C) 2000 RidgeRun, Inc.
|
|
|
|
* Author: RidgeRun, Inc.
|
|
|
|
* Greg Lonnon glonnon@ridgerun.com or info@ridgerun.com
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-10-16 16:27:34 +08:00
|
|
|
#include <linux/stddef.h>
|
|
|
|
#include <linux/types.h>
|
2007-07-30 06:36:13 +08:00
|
|
|
#include <linux/fs.h>
|
2007-10-16 16:27:34 +08:00
|
|
|
#include <linux/init.h>
|
2005-06-09 06:47:50 +08:00
|
|
|
#include <linux/miscdevice.h>
|
2007-10-16 16:27:34 +08:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/mm.h>
|
2009-10-10 23:36:34 +08:00
|
|
|
|
2016-12-25 03:46:01 +08:00
|
|
|
#include <linux/uaccess.h>
|
2012-10-08 10:27:32 +08:00
|
|
|
#include <mem_user.h>
|
2007-10-16 16:27:34 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* These are set in mmapper_init, which is called at boot time */
|
|
|
|
static unsigned long mmapper_size;
|
2007-10-16 16:27:34 +08:00
|
|
|
static unsigned long p_buf;
|
|
|
|
static char *v_buf;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-10-16 16:27:34 +08:00
|
|
|
static ssize_t mmapper_read(struct file *file, char __user *buf, size_t count,
|
|
|
|
loff_t *ppos)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-08-27 13:48:15 +08:00
|
|
|
return simple_read_from_buffer(buf, count, ppos, v_buf, mmapper_size);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2007-10-16 16:27:34 +08:00
|
|
|
static ssize_t mmapper_write(struct file *file, const char __user *buf,
|
|
|
|
size_t count, loff_t *ppos)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-08-27 13:48:15 +08:00
|
|
|
if (*ppos > mmapper_size)
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2011-01-13 08:59:28 +08:00
|
|
|
return simple_write_to_buffer(v_buf, mmapper_size, ppos, buf, count);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2010-04-27 22:24:23 +08:00
|
|
|
static long mmapper_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2007-10-16 16:27:34 +08:00
|
|
|
return -ENOIOCTLCMD;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2007-10-16 16:27:34 +08:00
|
|
|
static int mmapper_mmap(struct file *file, struct vm_area_struct *vma)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int ret = -EINVAL;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
if (vma->vm_pgoff != 0)
|
|
|
|
goto out;
|
2007-10-16 16:27:34 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
size = vma->vm_end - vma->vm_start;
|
2007-10-16 16:27:34 +08:00
|
|
|
if (size > mmapper_size)
|
|
|
|
return -EFAULT;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-10-16 16:27:34 +08:00
|
|
|
/*
|
|
|
|
* XXX A comment above remap_pfn_range says it should only be
|
2005-04-17 06:20:36 +08:00
|
|
|
* called when the mm semaphore is held
|
|
|
|
*/
|
|
|
|
if (remap_pfn_range(vma, vma->vm_start, p_buf >> PAGE_SHIFT, size,
|
2007-10-16 16:27:34 +08:00
|
|
|
vma->vm_page_prot))
|
2005-04-17 06:20:36 +08:00
|
|
|
goto out;
|
|
|
|
ret = 0;
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-10-16 16:27:34 +08:00
|
|
|
static int mmapper_open(struct inode *inode, struct file *file)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-16 16:27:34 +08:00
|
|
|
static int mmapper_release(struct inode *inode, struct file *file)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-09-27 16:50:33 +08:00
|
|
|
static const struct file_operations mmapper_fops = {
|
2005-04-17 06:20:36 +08:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.read = mmapper_read,
|
|
|
|
.write = mmapper_write,
|
2010-04-27 22:24:23 +08:00
|
|
|
.unlocked_ioctl = mmapper_ioctl,
|
2005-04-17 06:20:36 +08:00
|
|
|
.mmap = mmapper_mmap,
|
|
|
|
.open = mmapper_open,
|
|
|
|
.release = mmapper_release,
|
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-16 00:52:59 +08:00
|
|
|
.llseek = default_llseek,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2007-10-16 16:27:34 +08:00
|
|
|
/*
|
|
|
|
* No locking needed - only used (and modified) by below initcall and exitcall.
|
|
|
|
*/
|
2006-10-20 14:28:26 +08:00
|
|
|
static struct miscdevice mmapper_dev = {
|
2005-06-09 06:47:50 +08:00
|
|
|
.minor = MISC_DYNAMIC_MINOR,
|
|
|
|
.name = "mmapper",
|
|
|
|
.fops = &mmapper_fops
|
|
|
|
};
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static int __init mmapper_init(void)
|
|
|
|
{
|
2005-06-09 06:47:50 +08:00
|
|
|
int err;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
printk(KERN_INFO "Mapper v0.1\n");
|
|
|
|
|
|
|
|
v_buf = (char *) find_iomem("mmapper", &mmapper_size);
|
2007-10-16 16:27:34 +08:00
|
|
|
if (mmapper_size == 0) {
|
2005-04-17 06:20:36 +08:00
|
|
|
printk(KERN_ERR "mmapper_init - find_iomem failed\n");
|
2009-10-10 23:36:34 +08:00
|
|
|
return -ENODEV;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2009-10-10 23:36:34 +08:00
|
|
|
p_buf = __pa(v_buf);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-06-09 06:47:50 +08:00
|
|
|
err = misc_register(&mmapper_dev);
|
2007-10-16 16:27:34 +08:00
|
|
|
if (err) {
|
2005-06-09 06:47:50 +08:00
|
|
|
printk(KERN_ERR "mmapper - misc_register failed, err = %d\n",
|
|
|
|
err);
|
2011-04-09 10:49:08 +08:00
|
|
|
return err;
|
2005-06-09 06:47:50 +08:00
|
|
|
}
|
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2022-09-11 10:52:38 +08:00
|
|
|
static void __exit mmapper_exit(void)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2005-06-09 06:47:50 +08:00
|
|
|
misc_deregister(&mmapper_dev);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module_init(mmapper_init);
|
|
|
|
module_exit(mmapper_exit);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Greg Lonnon <glonnon@ridgerun.com>");
|
|
|
|
MODULE_DESCRIPTION("DSPLinux simulator mmapper driver");
|
2011-01-13 08:59:27 +08:00
|
|
|
MODULE_LICENSE("GPL");
|