Subject: ioctl_copy{in,out} proposal
To: YAMAMOTO Takashi <yamt@mwd.biglobe.ne.jp>
From: Reinoud Zandijk <reinoud@netbsd.org>
List: tech-kern
Date: 08/28/2005 15:48:05
--i9LlY+UWpKt15+FH
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Dear folks,

On Fri, Aug 26, 2005 at 05:14:08PM +0200, Reinoud Zandijk wrote:
> Maybe its better to keep it to ioctl_copyin() and ioctl_copyout() for that 
> are the places that its going to be usefull most. If other places have 
> similar issues we could create the stubs for them too.

now the updated and more complete patch for ioctl_copyin/ioctl_copyout. One 
might ask why such a feedback but consider it my learning curve for more 
consistent patches:

Index: sys/sys/systm.h
===================================================================
RCS file: /cvsroot/src/sys/sys/systm.h,v
retrieving revision 1.179
diff -u -r1.179 systm.h
--- sys/sys/systm.h	23 Jun 2005 00:30:28 -0000	1.179
+++ sys/sys/systm.h	28 Aug 2005 13:36:48 -0000
@@ -243,6 +243,9 @@
 int	copyin_proc(struct proc *, const void *, void *, size_t);
 int	copyout_proc(struct proc *, const void *, void *, size_t);
 
+int	ioctl_copyin(int ioctlflags, const void *src, void *dst, size_t len);
+int	ioctl_copyout(int ioctlflags, const void *src, void *dst, size_t len);
+
 int	subyte(void *, int);
 int	suibyte(void *, int);
 int	susword(void *, short);
Index: sys/kern/kern_subr.c
===================================================================
RCS file: /cvsroot/src/sys/kern/kern_subr.c,v
retrieving revision 1.118
diff -u -r1.118 kern_subr.c
--- sys/kern/kern_subr.c        6 Jul 2005 22:30:42 -0000       1.118
+++ sys/kern/kern_subr.c        28 Aug 2005 13:46:17 -0000
@@ -327,6 +327,30 @@
 }
 
 /*
+ * Like copyin(), except it operates on kernel addresses when the FKIOCTL
+ * flag is passed in `ioctlflags' from the ioctl call.
+ */
+int
+ioctl_copyin(int ioctlflags, const void *src, void *dst, size_t len)
+{
+       if (ioctlflags & FKIOCTL)
+               return kcopy(src, dst, len);
+       return copyin(src, dst, len);
+}
+
+/*
+ * Like copyout(), except it operates on kernel addresses when the FKIOCTL
+ * flag is passed in `ioctlflags' from the ioctl call.
+ */
+int
+ioctl_copyout(int ioctlflags, const void *src, void *dst, size_t len)
+{
+       if (ioctlflags & FKIOCTL)
+               return kcopy(src, dst, len);
+       return copyout(src, dst, len);
+}
+
+/*
  * General routine to allocate a hash table.
  * Allocate enough memory to hold at least `elements' list-head pointers.
  * Return a pointer to the allocated space and set *hashmask to a pattern
Index: 
share/man/man9/Makefile
===================================================================
RCS file: /cvsroot/src/share/man/man9/Makefile,v
retrieving revision 1.174
diff -u -r1.174 Makefile
--- share/man/man9/Makefile	23 Aug 2005 09:34:11 -0000	1.174
+++ share/man/man9/Makefile	28 Aug 2005 13:36:50 -0000
@@ -149,7 +149,8 @@
 	cons.9 cnputc.9
 MLINKS+=copy.9 copyin.9 copy.9 copyout.9 copy.9 copystr.9 \
 	copy.9 copyinstr.9 copy.9 copyoutstr.9 \
-	copy.9 copyin_proc.9 copy.9 copyout_proc.9
+	copy.9 copyin_proc.9 copy.9 copyout_proc.9 \
+	copy.9 ioctl_copyin.9 copy.9 ioctl_copyout.9
 MLINKS+=cpu_dumpconf.9 cpu_dump.9 cpu_dumpconf.9 cpu_dumpsize.9 \
 	cpu_dumpconf.9 dumpsys.9
 MLINKS+=cpu_fork.9 child_return.9 cpu_fork.9 proc_trampoline.9
Index: share/man/man9/copy.9
===================================================================
RCS file: /cvsroot/src/share/man/man9/copy.9,v
retrieving revision 1.14
diff -u -r1.14 copy.9
--- share/man/man9/copy.9	16 Apr 2003 13:35:26 -0000	1.14
+++ share/man/man9/copy.9	28 Aug 2005 13:36:50 -0000
@@ -60,6 +60,10 @@
 .Fn copyin_proc "struct proc *p" "const void *uaddr" "void *kaddr" "size_t len"
 .Ft int
 .Fn copyout_proc "struct proc *p" "const void *kaddr" "void *uaddr" "size_t len"
+.Ft int
+.Fn ioctl_copyin "int ioctlflags" "const void *src" "void *dst" "size_t len"
+.Ft int
+.Fn ioctl_copyout "int ioctlflags" "const void *src" "void *dst" "size_t len"
 .Sh DESCRIPTION
 The
 .Nm
@@ -138,6 +142,18 @@
 .Fn copyout ,
 except it operates on the address space of the process
 .Fa p .
+.It Fn ioctl_copyin
+Like
+.Fn copyin ,
+except it operates on kernel adresses when the FKIOCTL flag is passed in 
+.Fa ioctlflags
+from the ioctl call.
+.It Fn ioctl_copyout
+Like
+.Fn copyout ,
+except it operates on kernel adresses when the FKIOCTL flag is passed in 
+.Fa ioctlflags
+from the ioctl call.
 .El
 .Sh RETURN VALUES
 The
Index: distrib/sets/lists/comp/mi
===================================================================
RCS file: /cvsroot/src/distrib/sets/lists/comp/mi,v
retrieving revision 1.808
diff -u -r1.808 mi
--- distrib/sets/lists/comp/mi	23 Aug 2005 17:47:07 -0000	1.808
+++ distrib/sets/lists/comp/mi	28 Aug 2005 13:37:08 -0000
@@ -9161,6 +9161,8 @@
 ./usr/share/man/man9/ioasic_intr_evcnt.9	comp-sys-man		.man
 ./usr/share/man/man9/ioasic_submatch.9		comp-sys-man		.man
 ./usr/share/man/man9/ioctl.9			comp-sys-man		.man
+./usr/share/man/man9/ioctl_copyin.9		comp-sys-man		.man
+./usr/share/man/man9/ioctl_copyout.9		comp-sys-man		.man
 ./usr/share/man/man9/ipkdb.9			comp-sys-man		.man
 ./usr/share/man/man9/ipkdb_connect.9		comp-sys-man		.man
 ./usr/share/man/man9/ipkdb_init.9		comp-sys-man		.man
---------------------------------------

If noone objects, i'll commit it later on.

With regards,
Reinoud


--i9LlY+UWpKt15+FH
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (NetBSD)

iQEVAwUBQxHAjoKcNwBDyKpoAQLKDwf/cKKKBb2HHBQD1jHCax3vS19zTM9jHIBk
BinN7F156BXUVlYqFHWu9GJPqC+iPjXV9JpOlO4bebiYVLQjOmxdxPCC/wsKTRL0
HRcga8LgpKkIM5Oz9/BAswcbb4Uw1PcnwFWWx/ryjsrl+2lkmMmEq/EHkxXZN1cn
Y2Mudaao7HH6enQzkgmeqnUbjyLoUPwSBJUzn8dAqlhWDuMUonrPKIbCFo+IqSg9
XVRA4VR+JBrJ1I8SKlqK/ASXzOHGjWewLgLT0CJeloPNsX4Y3umniWSDw30cX6aB
vm8UbelAhaJjZoVpzyDEeGJEUGTCoAhgEsAmunZIxuZqKM8n1Z2bLg==
=VevV
-----END PGP SIGNATURE-----

--i9LlY+UWpKt15+FH--