mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Andrey Smirnov <andrew.smirnov@gmail.com>
To: barebox@lists.infradead.org
Cc: Andrey Smirnov <andrew.smirnov@gmail.com>
Subject: [PATCH v2 16/19] fs: Introduce mem_read_nofail()
Date: Mon, 28 Jan 2019 22:55:46 -0800	[thread overview]
Message-ID: <20190129065549.29161-17-andrew.smirnov@gmail.com> (raw)
In-Reply-To: <20190129065549.29161-1-andrew.smirnov@gmail.com>

Introduce mem_read_nofail() to allow memory reads of
unused/not-implmented memory addresses.

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 fs/devfs-core.c  | 64 ++++++++++++++++++++++++++++++++++++++++++------
 include/driver.h |  2 ++
 2 files changed, 58 insertions(+), 8 deletions(-)

diff --git a/fs/devfs-core.c b/fs/devfs-core.c
index b2f66578d..84234fd38 100644
--- a/fs/devfs-core.c
+++ b/fs/devfs-core.c
@@ -29,6 +29,7 @@
 #include <linux/mtd/mtd.h>
 #include <unistd.h>
 #include <fs.h>
+#include <abort.h>
 
 LIST_HEAD(cdev_list);
 
@@ -528,9 +529,39 @@ void cdev_remove_loop(struct cdev *cdev)
 	free(cdev);
 }
 
+static u64 mem_read_ptr(const void *ptr, int rwsize)
+{
+	switch (rwsize) {
+	case 1:
+		return *((u8 *)ptr);
+	case 2:
+		return *((u16 *)ptr);
+	case 4:
+		return *((u32 *)ptr);
+	case 8:
+		return *((u64 *)ptr);
+	}
+
+	BUG();
+	return 0;
+}
+
+static u64 mem_read_ptr_nofail(const void *ptr, int rwsize)
+{
+	u64 data;
+
+	data_abort_mask();
+	data = mem_read_ptr(ptr, rwsize);
+
+	if (data_abort_unmask())
+		return U64_MAX;
+
+	return data;
+}
+
 static ssize_t mem_copy(struct device_d *dev, void *dst, const void *src,
 			resource_size_t count, resource_size_t offset,
-			unsigned long flags)
+			unsigned long flags, bool nofail)
 {
 	ssize_t size;
 	int rwsize = flags & O_RWSIZE_MASK;
@@ -541,7 +572,7 @@ static ssize_t mem_copy(struct device_d *dev, void *dst, const void *src,
 	count = size = min(count, resource_size(&dev->resource[0]) - offset);
 
 	/* no rwsize specification given. Do whatever memcpy likes best */
-	if (!rwsize) {
+	if (!nofail && !rwsize) {
 		memcpy(dst, src, count);
 		goto out;
 	}
@@ -551,18 +582,22 @@ static ssize_t mem_copy(struct device_d *dev, void *dst, const void *src,
 	count = size = ALIGN_DOWN(count, rwsize);
 
 	while (count) {
+		const u64 data = nofail ?
+			mem_read_ptr_nofail(src, rwsize) :
+			mem_read_ptr(src, rwsize);
+
 		switch (rwsize) {
 		case 1:
-			*((u8 *)dst) = *((u8 *)src);
+			*((u8 *)dst) = data;
 			break;
 		case 2:
-			*((u16 *)dst) = *((u16 *)src);
+			*((u16 *)dst) = data;
 			break;
 		case 4:
-			*((u32  *)dst) = *((u32  *)src);
+			*((u32  *)dst) = data;
 			break;
 		case 8:
-			*((u64  *)dst) = *((u64  *)src);
+			*((u64  *)dst) = data;
 			break;
 		}
 		dst += rwsize;
@@ -573,6 +608,19 @@ out:
 	return size;
 }
 
+ssize_t mem_read_nofail(struct cdev *cdev, void *buf, size_t count,
+			loff_t offset, unsigned long flags)
+{
+	struct device_d *dev = cdev->dev;
+
+	if (!dev)
+		return -1;
+
+	return mem_copy(dev, buf, dev_get_mem_region(dev, 0) + offset,
+			count, offset, flags, true);
+}
+EXPORT_SYMBOL(mem_read_nofail);
+
 ssize_t mem_read(struct cdev *cdev, void *buf, size_t count, loff_t offset,
 		 unsigned long flags)
 {
@@ -582,7 +630,7 @@ ssize_t mem_read(struct cdev *cdev, void *buf, size_t count, loff_t offset,
 		return -1;
 
 	return mem_copy(dev, buf, dev_get_mem_region(dev, 0) + offset,
-			count, offset, flags);
+			count, offset, flags, false);
 }
 EXPORT_SYMBOL(mem_read);
 
@@ -595,6 +643,6 @@ ssize_t mem_write(struct cdev *cdev, const void *buf, size_t count,
 		return -1;
 
 	return mem_copy(dev, dev_get_mem_region(dev, 0) + offset, buf,
-			count, offset, flags);
+			count, offset, flags, false);
 }
 EXPORT_SYMBOL(mem_write);
diff --git a/include/driver.h b/include/driver.h
index 2db1cf1a8..270b3805d 100644
--- a/include/driver.h
+++ b/include/driver.h
@@ -350,6 +350,8 @@ struct driver_d *get_driver_by_name(const char *name);
 struct cdev;
 
 /* These are used by drivers which work with direct memory accesses */
+ssize_t mem_read_nofail(struct cdev *cdev, void *buf, size_t count,
+			loff_t offset, unsigned long flags);
 ssize_t mem_read(struct cdev *cdev, void *buf, size_t count, loff_t offset, ulong flags);
 ssize_t mem_write(struct cdev *cdev, const void *buf, size_t count, loff_t offset, ulong flags);
 int mem_memmap(struct cdev *cdev, void **map, int flags);
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2019-01-29  6:56 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-29  6:55 [PATCH v2 00/19] 32-bit lseek and /dev/mem fixes/improvements Andrey Smirnov
2019-01-29  6:55 ` [PATCH v2 01/19] commands: Move mem_parse_options() to lib/misc.c Andrey Smirnov
2019-01-29  6:55 ` [PATCH v2 02/19] commands: Get rid of mem_rw_buf Andrey Smirnov
2019-01-29  6:55 ` [PATCH v2 03/19] commands: Move /dev/mem driver to drivers/misc Andrey Smirnov
2019-01-29  6:55 ` [PATCH v2 04/19] nvmem: Do not use DEVFS_IS_CHARACTER_DEV Andrey Smirnov
2019-01-29  6:55 ` [PATCH v2 05/19] common: firmware: Don't use FILE_SIZE_STREAM directly Andrey Smirnov
2019-01-29  6:55 ` [PATCH v2 06/19] devfs: Fix incorrect error check for cdev->ops->lseek() Andrey Smirnov
2019-01-29  6:55 ` [PATCH v2 07/19] fs: Update FILE position in lseek() Andrey Smirnov
2019-01-29  6:55 ` [PATCH v2 08/19] fs: Drop trivial .lseek() implementaitons in FS drivers Andrey Smirnov
2019-01-29  6:55 ` [PATCH v2 09/19] devfs: Drop dev_lseek_default() Andrey Smirnov
2019-01-29  6:55 ` [PATCH v2 10/19] fs: devfs: Change .lseek callbacks to return 'int' Andrey Smirnov
2019-02-04 14:32   ` Sascha Hauer
2019-01-29  6:55 ` [PATCH v2 11/19] fs: Do not use IS_ERR_VALUE() to validate offset in lseek() Andrey Smirnov
2019-01-29  6:55 ` [PATCH v2 12/19] fs: Simplify new position calculation " Andrey Smirnov
2019-01-29  6:55 ` [PATCH v2 13/19] fs: Share code between mem_write()/mem_read() Andrey Smirnov
2019-01-29  6:55 ` [PATCH v2 14/19] fs: Avoid division in mem_copy() Andrey Smirnov
2019-01-29  6:55 ` [PATCH v2 15/19] fs: Report actual data processed by mem_copy() Andrey Smirnov
2019-01-29  6:55 ` Andrey Smirnov [this message]
2019-01-29  6:55 ` [PATCH v2 17/19] commands: md: Do not use memmap() Andrey Smirnov
2019-02-04 13:57   ` Sascha Hauer
2019-02-04 19:35     ` Andrey Smirnov
2019-01-29  6:55 ` [PATCH v2 18/19] drivers: mem: Create file to access second half of 64-bit memory Andrey Smirnov
2019-01-29  8:48   ` Sascha Hauer
2019-01-29 20:40     ` Andrey Smirnov
2019-01-29 21:09       ` Sam Ravnborg
2019-01-31 10:54       ` Peter Mamonov
2019-01-31 12:50         ` Peter Mamonov
2019-02-01  7:47           ` Sascha Hauer
2019-02-01 10:25             ` Peter Mamonov
2019-02-02  1:07               ` Andrey Smirnov
2019-02-02  0:35             ` Andrey Smirnov
2019-02-04  7:40               ` Sascha Hauer
2019-01-31 20:17         ` Andrey Smirnov
2019-02-01 10:14           ` Peter Mamonov
2019-01-29  6:55 ` [PATCH v2 19/19] libfile: Fix incorrect lseek check in open_and_lseek() Andrey Smirnov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190129065549.29161-17-andrew.smirnov@gmail.com \
    --to=andrew.smirnov@gmail.com \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox