[PATCH] Standardization of memcpy_form/tofs functions

Juan Perez-Sanchez <[email protected]>
Newsgroups org.kernel.vger.linux-8086
Message-ID <CAD6VGuYAucqZJ+BPmjNBkCT2LY4TrXSs2H31oZay_KBb97M11A@mail.gmail.com>
Hi,

Patch 3 of 5

Juan

PREVIOUS OPERATION

1. Several functions used memcpy_from/tofs to move 1, 2 or 4 bytes,
   instead of the more efficient get_user_*/put_user_*, so wasting
   memory.

2. Other functions did the opposite: transfer more than 4 bytes of memory
   using for loops with get_user_*/put_user_* instead of memcpy_from/tofs,
   again wasting memory.

3. Library function memmove() implemented and used a memory block move
   function (blt_back()), almost exactly the same as fmemcpy(), again
   wasting memory.

NEW OPERATION

1. All transfers to/from user space less than 4 bytes were modified to use
   only get_user_*/put_user_* functions.

2. All transfers to/from user space more than 4 bytes were modified to use
   only memcpy_from/tofs functions.

3. Removed blt_back() function and replaced it with fmemcpy().

4. In file "arch/i86/drivers/block/doshd.c", function "bioshd_geninit()"
   was rewritten to make it faster and smaller.

OTHER CHANGES

   Code size reduced by 80 bytes.

 The Image builded without errors. The kernel was tested with QEMU and
dioscuri emulators. Also in a PPro pc booting from floppy.
elksQ.patch (application/octet-stream, 6.4 KB)
diff -Nurb elks.orig/arch/i86/drivers/block/doshd.c elks/arch/i86/drivers/block/doshd.c
--- elks.orig/arch/i86/drivers/block/doshd.c	2012-10-17 13:34:52.000000000 -0500
+++ elks/arch/i86/drivers/block/doshd.c	2012-10-18 00:08:16.000000000 -0500
@@ -179,7 +179,7 @@
 
 #ifdef CONFIG_BLK_DEV_BFD
 
-unsigned short int bioshd_getfdinfo(void)
+static unsigned short int bioshd_getfdinfo(void)
 {
 
 #ifdef CONFIG_BLK_DEV_BFD_HARD
@@ -833,12 +833,11 @@
 static void bioshd_geninit(void)
 {
     register struct drive_infot *drivep;
-    register struct hd_struct *hdp;
+    register struct hd_struct *hdp = hd;
     int i;
 
     for (i = 0; i < 4 << 6; i++) {
 	drivep = &drive_info[i >> 6];
-	hdp = &hd[i];
 	if ((i & ((1 << 6) - 1)) == 0) {
 	    hdp->nr_sects = (sector_t) drivep->sectors *
 		drivep->heads * drivep->cylinders;
@@ -847,6 +846,7 @@
 	    hdp->nr_sects = 0;
 	    hdp->start_sect = -1;
 	}
+	hdp++;
     }
 
 #if 0
diff -Nurb elks.orig/arch/i86/drivers/block/floppy.c elks/arch/i86/drivers/block/floppy.c
--- elks.orig/arch/i86/drivers/block/floppy.c	2012-10-17 12:12:32.000000000 -0500
+++ elks/arch/i86/drivers/block/floppy.c	2012-10-17 23:36:57.000000000 -0500
@@ -1172,14 +1172,8 @@
 	    this_floppy = &floppy_type[drive >> 2];
 	else if ((this_floppy = current_type[drive & 3]) == NULL)
 	    return -ENODEV;
-	i =
-	    verify_area(VERIFY_WRITE, (void *) param,
-			sizeof(struct floppy_struct));
-	if (i)
-	    return i;
-	for (cnt = 0; cnt < sizeof(struct floppy_struct); cnt++)
-	    put_user_char(((char *) this_floppy)[cnt], (char *) param + cnt);
-	return 0;
+	return verified_memcpy_tofs((char *)param,
+		    (char *)this_floppy, sizeof(struct floppy_struct));
     case FDFMTTRK:
 	if (!suser())
 	    return -EPERM;
@@ -1188,8 +1182,8 @@
 	clr_irq();
 	while (format_status != FORMAT_NONE)
 	    sleep_on(&format_done);
-	for (cnt = 0; cnt < sizeof(struct format_descr); cnt++)
-	    ((char *) &format_req)[cnt] = get_user_char((char *) param + cnt);
+	memcpy_fromfs((char *)(&format_req),
+		    (char *)param, sizeof(struct format_descr));
 	format_req.device = drive;
 	format_status = FORMAT_WAIT;
 	format_errors = 0;
diff -Nurb elks.orig/arch/i86/drivers/char/mem.c elks/arch/i86/drivers/char/mem.c
--- elks.orig/arch/i86/drivers/char/mem.c	2012-08-18 13:28:59.000000000 -0500
+++ elks/arch/i86/drivers/char/mem.c	2012-10-17 23:36:57.000000000 -0500
@@ -172,18 +172,18 @@
 
     case MEM_GETMODTEXT:
 	i = (char *) module_init;
-	memcpy_tofs(arg, &i, 2);
+	put_user((unsigned short int)i, (void *)arg);
 	return 0;
     case MEM_GETMODDATA:
 	i = (char *) module_data;
-	memcpy_tofs(arg, &i, 2);
+	put_user((unsigned short int)i, (void *)arg);
 	return 0;
 
 #endif
 
     case MEM_GETTASK:
 	i = (char *) task;
-	memcpy_tofs(arg, &i, 2);
+	put_user((unsigned short int)i, (void *)arg);
 
 #if 0
 
@@ -197,11 +197,11 @@
 	return 0;
     case MEM_GETCS:
 	i = (char *) get_cs();
-	memcpy_tofs(arg, &i, 2);
+	put_user((unsigned short int)i, (void *)arg);
 	return 0;
     case MEM_GETDS:
 	i = (char *) get_ds();
-	memcpy_tofs(arg, &i, 2);
+	put_user((unsigned short int)i, (void *)arg);
 	return 0;
     case MEM_GETUSAGE:
 	mu.free_memory = mm_get_usage(MM_MEM, 0);
diff -Nurb elks.orig/arch/i86/kernel/bios16.c elks/arch/i86/kernel/bios16.c
--- elks.orig/arch/i86/kernel/bios16.c	2012-08-18 13:28:59.000000000 -0500
+++ elks/arch/i86/kernel/bios16.c	2012-10-18 00:18:09.000000000 -0500
@@ -56,7 +56,7 @@
 
 ! DS already saved in stashed_ds
 
-	mov bx, _bios_data_table
+	mov bx, #_bdt
 
 !	Load the register block from the table	
 
@@ -104,7 +104,7 @@
 
 ! ***** We can now use the bios data table again *****
 
- 	mov  bx, _bios_data_table
+	mov bx, #_bdt
 
  	pop 18[bx]         ! Save the old DS
  	mov 2[bx],ax       ! Save the old AX
diff -Nurb elks.orig/arch/i86/lib/memmove.c elks/arch/i86/lib/memmove.c
--- elks.orig/arch/i86/lib/memmove.c	2012-08-18 13:28:59.000000000 -0500
+++ elks/arch/i86/lib/memmove.c	2012-10-18 01:21:01.000000000 -0500
@@ -7,7 +7,6 @@
 
 #include <linuxmt/mm.h>
 
-static void blt_back(unsigned,unsigned,unsigned,unsigned,unsigned);
 static void blt_forth(unsigned,unsigned,unsigned,unsigned,unsigned);
 
 void far_memmove(unsigned sseg, unsigned soff, unsigned dseg, unsigned doff,
@@ -23,47 +22,14 @@
 	    --bytes;
 	    blt_forth(sseg, soff+bytes, dseg, doff+bytes, bytes+1);
 	} else {
-	    blt_back(sseg, soff, dseg, doff, bytes );
+	    fmemcpy(dseg, doff, sseg, soff, bytes );
 	}
     }
 }
 
 #ifndef S_SPLINT_S
 #asm
-				! blt_back( sseg, soff, dseg, doff, bytes )
-				! for to < from
 	.text
-	.even
-
-_blt_back:
-	push	bp
-	mov	bp, sp
-	push	ax
-	push	es
-	push	ds
-	push	cx	
-	push	si
-	push	di
-	pushf
-	mov	ax, [bp+4]
-	mov	ds, ax
-	mov	si, [bp+6]
-	mov	ax, [bp+8]
-	mov	es, ax
-	mov	di, [bp+10]
-	mov	cx, [bp+12]
-	cld
-	rep
-	movsb
-	popf
-	pop	di
-	pop	si
-	pop	cx
-	pop	ds
-	pop	es
-	pop	ax
-	pop	bp
-	ret
 				! blt_forth( sseg, soff, dseg, doff, bytes )
 				! for to > from 
 	.even
diff -Nurb elks.orig/fs/minix/file.c elks/fs/minix/file.c
--- elks.orig/fs/minix/file.c	2012-08-18 13:28:59.000000000 -0500
+++ elks/fs/minix/file.c	2012-10-17 23:36:57.000000000 -0500
@@ -154,9 +154,8 @@
 	    unmap_brelse(bh);
 	    buf += chars;
 	} else {
-	    char zero = 0;
 	    while (chars-- > 0)
-		memcpy_tofs(buf++, &zero, 1);
+		put_user_char((unsigned char)0, (void *)(buf++));
 	}
 	offset = 0;
     }
diff -Nurb elks.orig/fs/minix/symlink.c elks/fs/minix/symlink.c
--- elks.orig/fs/minix/symlink.c	2012-10-17 12:12:32.000000000 -0500
+++ elks/fs/minix/symlink.c	2012-10-17 23:36:57.000000000 -0500
@@ -89,8 +89,7 @@
 	register char *pi = 0;
 	while (((int)pi) < buflen && (c = bh->b_data[(int)pi])) {
 	    pi++;
-	    memcpy_tofs(buffer++, &c, 1);
-	    /* put_user_char(c,buffer++); */
+	    put_user_char(c,buffer++);
 	}
 	unmap_brelse(bh);
 	return (int)pi;
diff -Nurb elks.orig/fs/pipe.c elks/fs/pipe.c
--- elks.orig/fs/pipe.c	2012-08-18 13:28:59.000000000 -0500
+++ elks/fs/pipe.c	2012-10-18 01:22:24.000000000 -0500
@@ -34,7 +34,7 @@
 
     do {
 	if (!current->files.fd[(unsigned int) pfd]) {
-	    (void) clear_bit((unsigned int) pfd,
+	    clear_bit((unsigned int) pfd,
 			     &current->files.close_on_exec);
 	    return (int) pfd;
 	}
diff -Nurb elks.orig/fs/read_write.c elks/fs/read_write.c
--- elks.orig/fs/read_write.c	2012-10-17 12:12:32.000000000 -0500
+++ elks/fs/read_write.c	2012-10-17 23:36:57.000000000 -0500
@@ -53,7 +53,7 @@
 
     }
 
-    memcpy_tofs(p_offset, &tmp, 4);
+    put_user_long((unsigned long int)tmp, (void *)p_offset);
 
     return 0;
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.