git: b887b665ebc0 - main - nexus: Use resource_validate_map_request

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Thu, 23 Nov 2023 17:07:06 UTC
The branch main has been updated by jhb:

URL: https://cgit.FreeBSD.org/src/commit/?id=b887b665ebc044f246d261e8af543765b846ed38

commit b887b665ebc044f246d261e8af543765b846ed38
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2023-11-23 17:06:37 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2023-11-23 17:06:37 +0000

    nexus: Use resource_validate_map_request
    
    Reviewed by:    imp
    Differential Revision:  https://reviews.freebsd.org/D42724
---
 sys/arm64/arm64/nexus.c     | 18 +++++-------------
 sys/powerpc/powerpc/nexus.c | 22 +++++-----------------
 sys/riscv/riscv/nexus.c     | 18 +++++-------------
 sys/x86/x86/nexus.c         | 18 +++++-------------
 4 files changed, 20 insertions(+), 56 deletions(-)

diff --git a/sys/arm64/arm64/nexus.c b/sys/arm64/arm64/nexus.c
index dee86bce656a..7c4cb073c2ef 100644
--- a/sys/arm64/arm64/nexus.c
+++ b/sys/arm64/arm64/nexus.c
@@ -462,7 +462,8 @@ nexus_map_resource(device_t bus, device_t child, int type, struct resource *r,
     struct resource_map_request *argsp, struct resource_map *map)
 {
 	struct resource_map_request args;
-	rman_res_t end, length, start;
+	rman_res_t length, start;
+	int error;
 
 	/* Resources must be active to be mapped. */
 	if ((rman_get_flags(r) & RF_ACTIVE) == 0)
@@ -478,18 +479,9 @@ nexus_map_resource(device_t bus, device_t child, int type, struct resource *r,
 	}
 
 	resource_init_map_request(&args);
-	if (argsp != NULL)
-		bcopy(argsp, &args, imin(argsp->size, args.size));
-	start = rman_get_start(r) + args.offset;
-	if (args.length == 0)
-		length = rman_get_size(r);
-	else
-		length = args.length;
-	end = start + length - 1;
-	if (start > rman_get_end(r) || start < rman_get_start(r))
-		return (EINVAL);
-	if (end > rman_get_end(r) || end < start)
-		return (EINVAL);
+	error = resource_validate_map_request(r, argsp, &args, &start, &length);
+	if (error)
+		return (error);
 
 	map->r_vaddr = pmap_mapdev_attr(start, length, args.memattr);
 	map->r_bustag = &memmap_bus;
diff --git a/sys/powerpc/powerpc/nexus.c b/sys/powerpc/powerpc/nexus.c
index 03f16dfa9de0..c5f460baf44e 100644
--- a/sys/powerpc/powerpc/nexus.c
+++ b/sys/powerpc/powerpc/nexus.c
@@ -353,9 +353,9 @@ static int
 nexus_map_resource(device_t bus, device_t child, int type, struct resource *r,
     struct resource_map_request *argsp, struct resource_map *map)
 {
-
 	struct resource_map_request args;
-	rman_res_t end, length, start;
+	rman_res_t length, start;
+	int error;
 
 	/* Resources must be active to be mapped. */
 	if (!(rman_get_flags(r) & RF_ACTIVE))
@@ -371,21 +371,9 @@ nexus_map_resource(device_t bus, device_t child, int type, struct resource *r,
 	}
 
 	resource_init_map_request(&args);
-	if (argsp != NULL)
-		bcopy(argsp, &args, imin(argsp->size, args.size));
-
-	start = rman_get_start(r) + args.offset;
-	if (args.length == 0)
-		length = rman_get_size(r);
-	else
-		length = args.length;
-
-	end = start + length - 1;
-	if (start > rman_get_end(r) || start < rman_get_start(r))
-		return (EINVAL);
-
-	if (end > rman_get_end(r) || end < start)
-		return (EINVAL);
+	error = resource_validate_map_request(r, argsp, &args, &start, &length);
+	if (error)
+		return (error);
 
 	/*
 	 * If this is a memory resource, map it into the kernel.
diff --git a/sys/riscv/riscv/nexus.c b/sys/riscv/riscv/nexus.c
index 2e693e6cf655..936db3e548e9 100644
--- a/sys/riscv/riscv/nexus.c
+++ b/sys/riscv/riscv/nexus.c
@@ -424,7 +424,8 @@ nexus_map_resource(device_t bus, device_t child, int type, struct resource *r,
     struct resource_map_request *argsp, struct resource_map *map)
 {
 	struct resource_map_request args;
-	rman_res_t end, length, start;
+	rman_res_t length, start;
+	int error;
 
 	/* Resources must be active to be mapped. */
 	if ((rman_get_flags(r) & RF_ACTIVE) == 0)
@@ -440,18 +441,9 @@ nexus_map_resource(device_t bus, device_t child, int type, struct resource *r,
 	}
 
 	resource_init_map_request(&args);
-	if (argsp != NULL)
-		bcopy(argsp, &args, imin(argsp->size, args.size));
-	start = rman_get_start(r) + args.offset;
-	if (args.length == 0)
-		length = rman_get_size(r);
-	else
-		length = args.length;
-	end = start + length - 1;
-	if (start > rman_get_end(r) || start < rman_get_start(r))
-		return (EINVAL);
-	if (end > rman_get_end(r) || end < start)
-		return (EINVAL);
+	error = resource_validate_map_request(r, argsp, &args, &start, &length);
+	if (error)
+		return (error);
 
 	map->r_vaddr = pmap_mapdev(start, length);
 	map->r_bustag = &memmap_bus;
diff --git a/sys/x86/x86/nexus.c b/sys/x86/x86/nexus.c
index 4896271b92bd..54c655c4fafd 100644
--- a/sys/x86/x86/nexus.c
+++ b/sys/x86/x86/nexus.c
@@ -458,7 +458,8 @@ nexus_map_resource(device_t bus, device_t child, int type, struct resource *r,
     struct resource_map_request *argsp, struct resource_map *map)
 {
 	struct resource_map_request args;
-	rman_res_t end, length, start;
+	rman_res_t length, start;
+	int error;
 
 	/* Resources must be active to be mapped. */
 	if (!(rman_get_flags(r) & RF_ACTIVE))
@@ -474,18 +475,9 @@ nexus_map_resource(device_t bus, device_t child, int type, struct resource *r,
 	}
 
 	resource_init_map_request(&args);
-	if (argsp != NULL)
-		bcopy(argsp, &args, imin(argsp->size, args.size));
-	start = rman_get_start(r) + args.offset;
-	if (args.length == 0)
-		length = rman_get_size(r);
-	else
-		length = args.length;
-	end = start + length - 1;
-	if (start > rman_get_end(r) || start < rman_get_start(r))
-		return (EINVAL);
-	if (end > rman_get_end(r) || end < start)
-		return (EINVAL);
+	error = resource_validate_map_request(r, argsp, &args, &start, &length);
+	if (error)
+		return (error);
 
 	/*
 	 * If this is a memory resource, map it into the kernel.