Subject: mmap() and page alignment
To: None <tech-kern@NetBSD.ORG>
From: None <friedl@informatik.uni-kl.de>
List: tech-kern
Date: 07/06/1995 14:44:26
Whem mapping a file, the mmap() systems call requires the offset to be
page aligned.  This is not necessary.  Of course performance is better
if a memory page does not cross a filesystem block.  But the vnode pager
also works if the file offset is not page aligned.

Currently the Linux emulation reads in the exec'd files instead of
mapping them, because in Linux executables the pages are not aligned.
But it is better to have files mapped and shared between processes
than to read them for every process that uses them.

The following patch changes that.

--- vm/vm_mmap.c.org	Sun May 21 00:25:20 1995
+++ vm/vm_mmap.c	Sat Jun 24 23:11:05 1995
@@ -222,9 +222,12 @@
 
 	/*
 	 * Align the file position to a page boundary,
-	 * and save its page offset component.
+	 * and save its page offset component, if no fixed address.
 	 */
-	pageoff = (pos & PAGE_MASK);
+	if (flags & MAP_FIXED)
+		pageoff = 0;
+	else
+		pageoff = (pos & PAGE_MASK);
 	pos  -= pageoff;
 
 	/* Adjust size for rounding (on both ends). */
@@ -680,10 +683,6 @@
 
 	if (size == 0)
 		return (0);
-
-	/* The file offset must be page aligned. */
-	if (foff & PAGE_MASK)
-		return (EINVAL);
 
 	if ((flags & MAP_FIXED) == 0) {
 		/* The address is just a hint */
--- compat/linux/linux_exec.c~	Fri May 26 18:55:17 1995
+++ compat/linux/linux_exec.c	Sat Jun 24 23:14:58 1995
@@ -198,12 +198,12 @@
 	epp->ep_entry = execp->a_entry;
 
 	/* set up command for text segment */
-	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
+	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
 	    epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, ZMAGIC),
 	    VM_PROT_READ|VM_PROT_EXECUTE);
 
 	/* set up command for data segment */
-	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
+	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
 	    epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, ZMAGIC),
 	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
 
@@ -415,7 +415,7 @@
 	vcset.evs_used = 0;
 
 	NEW_VMCMD(&vcset,
-		  magic == ZMAGIC ? vmcmd_map_readvn : vmcmd_map_pagedvn,
+		  vmcmd_map_pagedvn,
 		  hdr.a_text + hdr.a_data, taddr,
 		  vp, LINUX_N_TXTOFF(hdr, magic),
 		  VM_PROT_READ|VM_PROT_EXECUTE|VM_PROT_WRITE);