1 /* $NetBSD: mm.h,v 1.2 2014/03/18 18:20:43 riastradh Exp $ */ 2 3 /*- 4 * Copyright (c) 2013 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Taylor R. Campbell. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #ifndef _LINUX_MM_H_ 33 #define _LINUX_MM_H_ 34 35 #include <sys/kauth.h> 36 #include <sys/file.h> 37 #include <sys/mman.h> 38 #include <sys/proc.h> 39 #include <sys/vnode.h> 40 41 #include <uvm/uvm_extern.h> 42 43 /* XXX Ugh bletch! Whattakludge! Linux's sense is reversed... */ 44 #undef PAGE_MASK 45 #define PAGE_MASK (~(PAGE_SIZE-1)) 46 47 #define PAGE_ALIGN(x) (((x) + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1)) 48 #define offset_in_page(x) ((x) & (PAGE_SIZE-1)) 49 50 /* 51 * ################################################################### 52 * ############### XXX THIS NEEDS SERIOUS SCRUTINY XXX ############### 53 * ################################################################### 54 */ 55 56 /* 57 * XXX unsigned long is a loser but will probably work accidentally. 58 * XXX struct file might not map quite right between Linux and NetBSD. 59 * XXX This is large enough it should take its own file. 60 */ 61 62 static inline unsigned long 63 vm_mmap(struct file *file, unsigned long base, unsigned long size, 64 unsigned long prot, unsigned long flags, unsigned long token) 65 { 66 struct vnode *vnode; 67 vaddr_t addr; 68 int error; 69 70 /* 71 * Cargo-culted from sys_mmap. Various conditions kasserted 72 * rather than checked for expedience and safey. 73 */ 74 75 KASSERT(base == 0); 76 KASSERT(prot == (PROT_READ | PROT_WRITE)); 77 KASSERT(flags == MAP_SHARED); 78 79 KASSERT(file->f_type == DTYPE_VNODE); 80 vnode = file->f_data; 81 82 KASSERT(vnode->v_type == VCHR); 83 KASSERT((file->f_flag & (FREAD | FWRITE)) == (FREAD | FWRITE)); 84 85 { 86 struct vattr va; 87 88 vn_lock(vnode, (LK_SHARED | LK_RETRY)); 89 error = VOP_GETATTR(vnode, &va, kauth_cred_get()); 90 VOP_UNLOCK(vnode); 91 if (error) 92 goto out; 93 /* XXX kassert? */ 94 if ((va.va_flags & (SF_SNAPSHOT | IMMUTABLE | APPEND)) != 0) { 95 error = EACCES; 96 goto out; 97 } 98 } 99 100 /* XXX pax_mprotect? pax_aslr? */ 101 102 addr = (*curproc->p_emul->e_vm_default_addr)(curproc, 103 (vaddr_t)curproc->p_vmspace->vm_daddr, size); 104 error = uvm_mmap(&curproc->p_vmspace->vm_map, &addr, size, 105 (VM_PROT_READ | VM_PROT_WRITE), (VM_PROT_READ | VM_PROT_WRITE), 106 MAP_SHARED, vnode, base, 107 curproc->p_rlimit[RLIMIT_MEMLOCK].rlim_cur); 108 if (error) 109 goto out; 110 111 KASSERT(addr <= -1024UL); /* XXX Kludgerosity! */ 112 113 out: /* XXX errno NetBSD->Linux (kludgerific) */ 114 return (error? (-error) : (unsigned long)addr); 115 } 116 117 #endif /* _LINUX_MM_H_ */ 118